Programación Web y Scripting > PHP
Obtener Dia, Mes Y AÑo Por Separado De Un Registro
Altareum:
Si existe.. strtotime
http://ar2.php.net/manual/es/function.strtotime.php
Convierte la fecha a timestamp.
Altareum.
XDim:
Hola, yo propondria otra manera de ver las fechas. En un principio, el tipo de campo de la tabla lo cambiaría a varchar en lugar de fecha. El motivo por el cual haría eso es que de cualquier cadena, es facilmente convertible a integer, date, etc. Ese campo podria almacenar, por ejemplo, hasta 20 caracteres.
Una vez que tuviese el campo como varchar, cuando fuese a insertar o updatear uno o varios registros, antes pasaria la fecha a un numero con la funcion mktime() que proporciona PHP. Esta funcion devuelve el numero de segundos que ha pasado desde Epoch UNIX (Enero 1 1970 00:00:00 GMT). Claro esta, si la fecha de nacimiento es anterior, deberia hacerse una funcion específica o aplicar al campo de la tabla el tipo de fecha (aunque eso sería una pena :unsure: ).
Para recuperarla, solo bastaria usar la funcion date(), pasando como segundo parámetro el campo del recordset o una variable que lleve el valor del campo del recordset.
Ahora os pongo un ejemplo de como quedaría mas o menos el asunto.
--- Código: Text --- <?php /* ***************************************** - De DD/MM/AAAA a NUMERO ****************************************** */ $dia = "01"; $mes = "07"; $ano = "2006"; $fecha_empleado_segundos = convertir_a_fecha ($dia, $mes, $ano); echo "La fecha de nacimiento del empleado R. Cartman es $dia/$mes/$ano (-> $fecha_empleado_segundos)<br>"; /* ***************************************** - De NUMERO a DD/MM/AAAA ****************************************** */ // ... // La siguiente linia se deberia borrar o comentar. Solo la pongo para asignar un valor $rs_empleado['fechanac_emp'] = $fecha_empleado_segundos; // ... $fecha_empleado = $rs_empleado['fechanac_emp']; // Se obtiene un numero semejante a 1555645468000 $fecha_empleado = coger_fecha_con_barras($fecha_empleado); echo "El recordset nos devuelve: ".$rs_empleado['fechanac_emp'].", que es el equivalente a $fecha_empleado<br>";?><?php function coger_fecha_con_barras($fecha){ $fecha = date ("d/m/Y", $fecha); /* //Tambien se podria haber puesto de la siguiente manera. Pero para este caso no es tan "bonito" $dia = date("d", $fecha); $mes = date("m", $fecha); $ano = date("Y", $fecha); $fecha = "$dia/$mes/$ano" */ return $fecha; } function convertir_a_fecha($dia, $mes, $ano){ // En esta funcion no se contempla la hora, el minuto ni el segundo, aunque siempre se puede adaptar return mktime(0, 0, 0, $mes, $dia, $ano); }?>
Bueno, una vez dicho esto, espero que a alguien le pueda servir.
Hasta pronto. :D
MaoBike:
mira para estraer la fecha por separado de la base de datos es muy facil usa el
--- Código: Text --- select to_char(fecha,'dd'),to_char(fecha,'mm'),to_char(fecha,'yyyy') from tabla
F_Tanori:
Bueno, ya que la discusion esta activada :whistling:
Quisiera hacer una aclaracion, en MySQL ( que segun veo es la base de datos que se utiliza) no existe la funcion to_char que es al estilo oracle
El equivalente puede ser DATE FORMAT
<!--sql--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td>SQL </td></tr><tr><td id='CODE'><!--sql1-->[color=blue;font-weight]SELECT[/color] DATE_FORMAT(fecha,'%d'),DATE_FORMAT(fecha,'%m'),DATE_FORMAT(fecha,'%Y') FROM tabla <!--sql2--></td></tr></table><div class='postcolor'><!--sql3-->
[dohtml]
<table border='1'>
<tr style="height: 12.75pt;" height="17">
<td class="xl2313238" style="height: 12.75pt; width: 48pt;" height="17" width="64">Specifier</td>
<td class="xl2313238" style="border-left: medium none; width: 245pt;" width="327">Description</td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl2413238" style="border-top: medium none; height: 15pt; width: 48pt;" height="20" width="64">%a</td>
<td class="xl2213238" style="border-top: medium none; border-left: medium none; width: 245pt;" width="327">Abbreviated weekday name (<font class="font513238">Sun</font><font class="font013238">..</font><font class="font513238">Sat</font><font class="font013238">)</font></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl2413238" style="border-top: medium none; height: 15pt; width: 48pt;" height="20" width="64">%b</td>
<td class="xl2213238" style="border-top: medium none; border-left: medium none; width: 245pt;" width="327">Abbreviated month name (<font class="font513238">Jan</font><font class="font013238">..</font><font class="font513238">Dec</font><font class="font013238">)</font></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl2413238" style="border-top: medium none; height: 15pt; width: 48pt;" height="20" width="64">%c</td>
<td class="xl2213238" style="border-top: medium none; border-left: medium none; width: 245pt;" width="327">Month, numeric (<font class="font513238">0</font><font class="font013238">..</font><font class="font513238">12</font><font class="font013238">)</font></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl2413238" style="border-top: medium none; height: 15pt; width: 48pt;" height="20" width="64">%D</td>
<td class="xl2213238" style="border-top: medium none; border-left: medium none; width: 245pt;" width="327">Day of the month with English suffix (<font class="font513238">0th</font><font class="font013238">, </font><font class="font513238">1st</font><font class="font013238">, </font><font class="font513238">2nd</font><font class="font013238">, </font><font class="font513238">3rd</font><font class="font013238">, ...)</font></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl2413238" style="border-top: medium none; height: 15pt; width: 48pt;" height="20" width="64">%d</td>
<td class="xl2213238" style="border-top: medium none; border-left: medium none; width: 245pt;" width="327">Day of the month, numeric (<font class="font513238">00</font><font class="font013238">..</font><font class="font513238">31</font><font class="font013238">)</font></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl2413238" style="border-top: medium none; height: 15pt; width: 48pt;" height="20" width="64">%e</td>
<td class="xl2213238" style="border-top: medium none; border-left: medium none; width: 245pt;" width="327">Day of the month, numeric (<font class="font513238">0</font><font class="font013238">..</font><font class="font513238">31</font><font class="font013238">)</font></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl2413238" style="border-top: medium none; height: 15pt; width: 48pt;" height="20" width="64">%f</td>
<td class="xl2213238" style="border-top: medium none; border-left: medium none; width: 245pt;" width="327">Microseconds (<font class="font513238">000000</font><font class="font013238">..</font><font class="font513238">999999</font><font class="font013238">)</font></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl2413238" style="border-top: medium none; height: 15pt; width: 48pt;" height="20" width="64">%H</td>
<td class="xl2213238" style="border-top: medium none; border-left: medium none; width: 245pt;" width="327">Hour (<font class="font513238">00</font><font class="font013238">..</font><font class="font513238">23</font><font class="font013238">)</font></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl2413238" style="border-top: medium none; height: 15pt; width: 48pt;" height="20" width="64">%h</td>
<td class="xl2213238" style="border-top: medium none; border-left: medium none; width: 245pt;" width="327">Hour (<font class="font513238">01</font><font class="font013238">..</font><font class="font513238">12</font><font class="font013238">)</font></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl2413238" style="border-top: medium none; height: 15pt; width: 48pt;" height="20" width="64">%I</td>
<td class="xl2213238" style="border-top: medium none; border-left: medium none; width: 245pt;" width="327">Hour (<font class="font513238">01</font><font class="font013238">..</font><font class="font513238">12</font><font class="font013238">)</font></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl2413238" style="border-top: medium none; height: 15pt; width: 48pt;" height="20" width="64">%i</td>
<td class="xl2213238" style="border-top: medium none; border-left: medium none; width: 245pt;" width="327">Minutes, numeric (<font class="font513238">00</font><font class="font013238">..</font><font class="font513238">59</font><font class="font013238">)</font></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl2413238" style="border-top: medium none; height: 15pt; width: 48pt;" height="20" width="64">%j</td>
<td class="xl2213238" style="border-top: medium none; border-left: medium none; width: 245pt;" width="327">Day of year (<font class="font513238">001</font><font class="font013238">..</font><font class="font513238">366</font><font class="font013238">)</font></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl2413238" style="border-top: medium none; height: 15pt; width: 48pt;" height="20" width="64">%k</td>
<td class="xl2213238" style="border-top: medium none; border-left: medium none; width: 245pt;" width="327">Hour (<font class="font513238">0</font><font class="font013238">..</font><font class="font513238">23</font><font class="font013238">)</font></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl2413238" style="border-top: medium none; height: 15pt; width: 48pt;" height="20" width="64">%l</td>
<td class="xl2213238" style="border-top: medium none; border-left: medium none; width: 245pt;" width="327">Hour (<font class="font513238">1</font><font class="font013238">..</font><font class="font513238">12</font><font class="font013238">)</font></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl2413238" style="border-top: medium none; height: 15pt; width: 48pt;" height="20" width="64">%M</td>
<td class="xl2213238" style="border-top: medium none; border-left: medium none; width: 245pt;" width="327">Month name (<font class="font513238">January</font><font class="font013238">..</font><font class="font513238">December</font><font class="font013238">)</font></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl2413238" style="border-top: medium none; height: 15pt; width: 48pt;" height="20" width="64">%m</td>
<td class="xl2213238" style="border-top: medium none; border-left: medium none; width: 245pt;" width="327">Month, numeric (<font class="font513238">00</font><font class="font013238">..</font><font class="font513238">12</font><font class="font013238">)</font></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl2413238" style="border-top: medium none; height: 15pt; width: 48pt;" height="20" width="64">%p</td>
<td class="xl2413238" style="border-top: medium none; border-left: medium none; width: 245pt;" width="327">AM<font class="font013238"> or </font><font class="font513238">PM</font></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl2413238" style="border-top: medium none; height: 15pt; width: 48pt;" height="20" width="64">%r</td>
<td class="xl2213238" style="border-top: medium none; border-left: medium none; width: 245pt;" width="327">Time, 12-hour (<font class="font513238">hh:mm:ss</font><font class="font013238"> followed by </font><font class="font513238">AM</font><font class="font013238"> or </font><font class="font513238">PM</font><font class="font013238">)</font></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl2413238" style="border-top: medium none; height: 15pt; width: 48pt;" height="20" width="64">%S</td>
<td class="xl2213238" style="border-top: medium none; border-left: medium none; width: 245pt;" width="327">Seconds (<font class="font513238">00</font><font class="font013238">..</font><font class="font513238">59</font><font class="font013238">)</font></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl2413238" style="border-top: medium none; height: 15pt; width: 48pt;" height="20" width="64">%s</td>
<td class="xl2213238" style="border-top: medium none; border-left: medium none; width: 245pt;" width="327">Seconds (<font class="font513238">00</font><font class="font013238">..</font><font class="font513238">59</font><font class="font013238">)</font></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl2413238" style="border-top: medium none; height: 15pt; width: 48pt;" height="20" width="64">%T</td>
<td class="xl2213238" style="border-top: medium none; border-left: medium none; width: 245pt;" width="327">Time, 24-hour (<font class="font513238">hh:mm:ss</font><font class="font013238">)</font></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl2413238" style="border-top: medium none; height: 15pt; width: 48pt;" height="20" width="64">%U</td>
<td class="xl2213238" style="border-top: medium none; border-left: medium none; width: 245pt;" width="327">Week (<font class="font513238">00</font><font class="font013238">..</font><font class="font513238">53</font><font class="font013238">), where Sunday is the
first day of the week</font></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl2413238" style="border-top: medium none; height: 15pt; width: 48pt;" height="20" width="64">%u</td>
<td class="xl2213238" style="border-top: medium none; border-left: medium none; width: 245pt;" width="327">Week (<font class="font513238">00</font><font class="font013238">..</font><font class="font513238">53</font><font class="font013238">), where Monday is the
first day of the week</font></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl2413238" style="border-top: medium none; height: 15pt; width: 48pt;" height="20" width="64">%V</td>
<td class="xl2213238" style="border-top: medium none; border-left: medium none; width: 245pt;" width="327">Week (<font class="font513238">01</font><font class="font013238">..</font><font class="font513238">53</font><font class="font013238">), where Sunday is the
first day of the week; used with </font><font class="font513238">%X</font></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl2413238" style="border-top: medium none; height: 15pt; width: 48pt;" height="20" width="64">%v</td>
<td class="xl2213238" style="border-top: medium none; border-left: medium none; width: 245pt;" width="327">Week (<font class="font513238">01</font><font class="font013238">..</font><font class="font513238">53</font><font class="font013238">), where Monday is the
first day of the week; used with </font><font class="font513238">%x</font></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl2413238" style="border-top: medium none; height: 15pt; width: 48pt;" height="20" width="64">%W</td>
<td class="xl2213238" style="border-top: medium none; border-left: medium none; width: 245pt;" width="327">Weekday name (<font class="font513238">Sunday</font><font class="font013238">..</font><font class="font513238">Saturday</font><font class="font013238">)</font></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl2413238" style="border-top: medium none; height: 15pt; width: 48pt;" height="20" width="64">%w</td>
<td class="xl2213238" style="border-top: medium none; border-left: medium none; width: 245pt;" width="327">Day of the week (<font class="font513238">0</font><font class="font013238">=Sunday..</font><font class="font513238">6</font><font class="font013238">=Saturday)</font></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl2413238" style="border-top: medium none; height: 15pt; width: 48pt;" height="20" width="64">%X</td>
<td class="xl2213238" style="border-top: medium none; border-left: medium none; width: 245pt;" width="327">Year for the week where Sunday is the first day of the week,
numeric, 4 digits; used with <font class="font513238">%V</font></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl2413238" style="border-top: medium none; height: 15pt; width: 48pt;" height="20" width="64">%x</td>
<td class="xl2213238" style="border-top: medium none; border-left: medium none; width: 245pt;" width="327">Year for the week, where Monday is the first day of the week,
numeric, 4 digits; used with <font class="font513238">%v</font></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl2413238" style="border-top: medium none; height: 15pt; width: 48pt;" height="20" width="64">%Y</td>
<td class="xl2213238" style="border-top: medium none; border-left: medium none; width: 245pt;" width="327">Year, numeric, 4 digits</td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl2413238" style="border-top: medium none; height: 15pt; width: 48pt;" height="20" width="64">%y</td>
<td class="xl2213238" style="border-top: medium none; border-left: medium none; width: 245pt;" width="327">Year, numeric, 2 digits</td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl2413238" style="border-top: medium none; height: 15pt; width: 48pt;" height="20" width="64">%%</td>
<td class="xl2213238" style="border-top: medium none; border-left: medium none; width: 245pt;" width="327">A literal <font class="font513238">`%'</font><font class="font013238">.</font>
</td>
</tr>
</table>
[/dohtml]
Saludos
Navegación
[*] Página Anterior
Ir a la versión completa