• Miércoles 1 de Mayo de 2024, 02:11

Autor Tema:  Obtener Dia, Mes Y AÑo Por Separado De Un Registro  (Leído 7871 veces)

martinshz

  • Nuevo Miembro
  • *
  • Mensajes: 3
    • Ver Perfil
Obtener Dia, Mes Y AÑo Por Separado De Un Registro
« en: Viernes 11 de Agosto de 2006, 22:28 »
0
Hola amigos, les tengo la siguiente consulta:

 ¿Como puedo recuperar el valor del dia, mes y año de una variable
que contiene la informacion de una fecha con funciones de fecha (no usar SUBSTR)

------------------------------------CODIGO--------------------------------
// Hago el recordset para manipular datos

$query_rs_empleados ="select * from empleados"
$rs_empleados = mysql_query($query_rs_empleados);
$row_rs_empleados = mysql_fetch_assoc($rs_empleados);

// aca obtengo una fecha, de la cual deseo obtener los datos de dia, mes y año
// en tres variables distintas

echo $row_rs_empleados['fechanac_emp']
--------------------------------FIN DE CODIGO---------------------------

Agradeciendoles de antemano su ayuda, gracias

VeRaCrUzAnItO-10-

  • Miembro activo
  • **
  • Mensajes: 88
    • Ver Perfil
Re: Obtener Dia, Mes Y AÑo Por Separado De Un Registro
« Respuesta #1 en: Sábado 12 de Agosto de 2006, 04:35 »
0
Pues de la variable q obtengas fijate como te la devuelve y de ahi cortala, hay una funcion en php para poder cortar una cadena, ahora no la recuerdo

Aca hay un ejemplo http://www.programacionweb.net/articulos/articulo/?num=112

la funcion split es la q necesitas creo o busca en ww.php.net

Saludos.

martinshz

  • Nuevo Miembro
  • *
  • Mensajes: 3
    • Ver Perfil
Re: Obtener Dia, Mes Y AÑo Por Separado De Un Registro
« Respuesta #2 en: Sábado 12 de Agosto de 2006, 16:40 »
0
Gracias por la respuesta:

Pero mi interrogante es justamente como hacerlo con  funciones de fecha
y no con funciones de cadena. gracias amigo

Informix

  • Miembro MUY activo
  • ***
  • Mensajes: 154
    • Ver Perfil
Re: Obtener Dia, Mes Y AÑo Por Separado De Un Registro
« Respuesta #3 en: Domingo 13 de Agosto de 2006, 18:51 »
0
Hola generalmente la fecha se maneja del modo YYYY-MM-DD no estoy seguro si en php existe una funcion especifica para extraer el dia, mes o año para cada variable, pero puedes usar la funcion ereg y obtener el dia, fecha y año en un array que es lo mismo que si utilizaras 3 variables distintas  :o para guardar dia, mes o año

por ejemplo usando tu codigo:

Código: Text
  1.  
  2. // Hago el recordset para manipular datos
  3.  
  4. $query_rs_empleados ="select * from empleados"
  5. $rs_empleados = mysql_query($query_rs_empleados);
  6. $row_rs_empleados = mysql_fetch_assoc($rs_empleados);
  7.  
  8. // aca obtengo una fecha, de la cual deseo obtener los datos de dia, mes y año
  9. // en tres variables distintas
  10.  
  11. echo $row_rs_empleados['fechanac_emp'];
  12.  
  13. $fecha = ereg("-", $row_rs_empleados['fechanac_emp']);
  14. echo $fecha[0]."<br><br>";
  15. echo $fecha[1]."<br><br>";
  16. echo $fecha[2]."<br><br>";
  17.  
  18.  

si $row_rs_empleados['fechanac_emp'] contiene el formato de fecha YYYY-MM-DD va a buscar el - y cada vez que lo encuentre va a crear un elemento en el array de esta manera $fecha[0] seria el año, $fecha[1] el mes y finalmente $fecha[2] el dia

espero que te sea de ayuda

Te recuerdo que como programador no siempre se tienen a la mano las funciones que necesitamos para crear alguna tarea y tenemos que hechar mano de otras funciones para llegar al objetivo, saludos
Informix

"Ganar no es algo momentáneo, es algo permanente. Uno no gana de vez en cuando, uno no hace las cosas bien a veces, uno hace las cosas bien siempre" .

Vince Lombardi


martinshz

  • Nuevo Miembro
  • *
  • Mensajes: 3
    • Ver Perfil
Re: Obtener Dia, Mes Y AÑo Por Separado De Un Registro
« Respuesta #4 en: Martes 15 de Agosto de 2006, 00:55 »
0
Gracias Informix

La alternativa me parece muy interesante, y la funcion es muy potente, tienes razon al decir que si no tenemos funciones en algunos leguajes de programacion bueno echemos manos de otras funciones. Mi gran curiosidad era saber si en PHP habian una funcion nativa para obtener el valor del MES, DIA y AÑO pero parece que no.

Gracias a todos

Altareum

  • Miembro MUY activo
  • ***
  • Mensajes: 327
    • Ver Perfil
    • http://www.avian.com.ar
Re: Obtener Dia, Mes Y AÑo Por Separado De Un Registro
« Respuesta #5 en: Martes 15 de Agosto de 2006, 19:36 »
0
Si existe.. strtotime

http://ar2.php.net/manual/es/function.strtotime.php

Convierte la fecha a timestamp.




Altareum.
El postre!, el postre!, el postre!, el postre!, el postre!, el postre!.....
 | Klinka | Kiree | Avian |

XDim

  • Nuevo Miembro
  • *
  • Mensajes: 1
    • Ver Perfil
Re: Obtener Dia, Mes Y AÑo Por Separado De Un Registro
« Respuesta #6 en: Miércoles 16 de Agosto de 2006, 01:50 »
0
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
  1.  
  2.  
  3. <?php
  4.  
  5.   /* *****************************************
  6.     - De DD/MM/AAAA a NUMERO
  7.   ****************************************** */  
  8.  
  9.   $dia = "01";
  10.   $mes = "07";
  11.   $ano = "2006";
  12.  
  13.   $fecha_empleado_segundos = convertir_a_fecha ($dia, $mes, $ano);
  14.  
  15.   echo "La fecha de nacimiento del empleado R. Cartman es $dia/$mes/$ano (-> $fecha_empleado_segundos)<br>";
  16.  
  17.   /* *****************************************
  18.     - De NUMERO a DD/MM/AAAA
  19.   ****************************************** */
  20.   // ...
  21.     // La siguiente linia se deberia borrar o comentar. Solo la pongo para asignar un valor
  22.     $rs_empleado['fechanac_emp'] = $fecha_empleado_segundos;
  23.   // ...
  24.   $fecha_empleado = $rs_empleado['fechanac_emp']; // Se obtiene un numero semejante a 1555645468000
  25.  
  26.   $fecha_empleado = coger_fecha_con_barras($fecha_empleado);
  27.  
  28.   echo "El recordset nos devuelve: ".$rs_empleado['fechanac_emp'].", que es el equivalente a $fecha_empleado<br>";
  29. ?>
  30. <?php
  31.  
  32.   function coger_fecha_con_barras($fecha){
  33.        
  34.     $fecha = date ("d/m/Y", $fecha);
  35.    
  36.     /*
  37.       //Tambien se podria haber puesto de la siguiente manera. Pero para este caso no es tan "bonito"
  38.    
  39.       $dia = date("d", $fecha);
  40.       $mes = date("m", $fecha);
  41.       $ano = date("Y", $fecha);
  42.      
  43.       $fecha = "$dia/$mes/$ano"
  44.     */
  45.    
  46.     return $fecha;
  47.   }
  48.  
  49.   function convertir_a_fecha($dia, $mes, $ano){
  50.     // En esta funcion no se contempla la hora, el minuto ni el segundo, aunque siempre se puede adaptar
  51.     return mktime(0, 0, 0, $mes, $dia, $ano);
  52.   }
  53. ?>
  54.  
  55.  
  56.  

Bueno, una vez dicho esto, espero que a alguien le pueda servir.

Hasta pronto.  :D
Solo hay 10 tipos de personas, las que saben binario y las que no.

MaoBike

  • Nuevo Miembro
  • *
  • Mensajes: 8
    • Ver Perfil
Re: Obtener Dia, Mes Y AÑo Por Separado De Un Registro
« Respuesta #7 en: Viernes 31 de Agosto de 2007, 23:13 »
0
mira para estraer la fecha por separado de la base de datos es muy facil usa el
Código: Text
  1.  
  2. select to_char(fecha,'dd'),to_char(fecha,'mm'),to_char(fecha,'yyyy') from tabla
  3.  
  4.  
[size=109]The Real Extreme Bikers
[/size] [size=109] El Cielo Es El Limite[/size]

F_Tanori

  • Moderador
  • ******
  • Mensajes: 1919
  • Nacionalidad: mx
    • Ver Perfil
Re: Obtener Dia, Mes Y AÑo Por Separado De Un Registro
« Respuesta #8 en: Sábado 1 de Septiembre de 2007, 01:08 »
0
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
" ExIsTo y A vEcEs PiEnSo "

NOTA:
===========================================================================================================================
Este foro es para ayudar, aprender, compartir... usenlo para eso,
NO SE RESUELVEN DUDAS POR MENSAJE PRIVADO Y MENOS POR CORREO
===========================================================================================================================