Foro de elhacker.net

Programación => PHP => Mensaje iniciado por: tomasvreal28 en 2 Diciembre 2015, 02:15 am



Título: PHP Cambiar fecha cadena a date
Publicado por: tomasvreal28 en 2 Diciembre 2015, 02:15 am
Buenas he encontrado un campo fecha pero esta en varchar  pero no puedo cambiarlo a date por que se perderían registros

Tengo que hacer un listado que me muestre ( Mes y Año )
Por ejemplo
con este formato Abril del 2015
 
podrían ayudarme como se puede convertir en mes y año de una cadena
las fechas estas guardadas así
Año-Mes-Dias
2015-12-24

Dejo comentado en donde se agrega el codigo
<!--- 
<td width="25" align=center><?php echo $res['icr_fecha_revision1'];?></td>

si lo hago de esta manera me  sale asi 2015-12-24
Pero lo que quiero es que salga Diciembre del 2015                                                                     
--->

Código
  1.  
  2.  
  3. <?php
  4. require_once('conexion.php');
  5.  
  6. if (isset($_GET['busc'])){
  7. $fecha_reporte=$_GET['fecha_reporte'];
  8. $fecha_reporte2=$_GET['fecha_reporte2'];
  9. $sql="select * from tbldocumentosicr INNER JOIN tblinfracciones ON tbldocumentosicr.icr_infraccion=tblinfracciones.icr_infraccion where icr_fecha_ri>='$fecha_reporte' and icr_fecha_ri<='$fecha_reporte2' order by icr_fecha_ri ASC";
  10. }else{
  11. $sql="select * from tbldocumentosicr INNER JOIN tblinfracciones ON tbldocumentosicr.icr_infraccion=tblinfracciones.icr_infraccion where icr_estado='5' order by icr_fecha_ri ASC";
  12. }
  13. $reg=mysql_query($sql,$cnx);
  14. ?>
  15. <html>
  16. <head>
  17. <title>Listado</title>
  18. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  19.    <script type="text/javascript" src="calendar.js"></script>
  20.    <script type="text/javascript" src="calendar-setup.js"></script>
  21.    <script type="text/javascript" src="calendar-es.js"></script>
  22.    <style type="text/css"> @import url("calendar-win2k-cold-1.css"); </style>
  23.  
  24. <script type="text/javascript" language="javascript">
  25.   function cambiar(id,color){
  26.    document.getElementById(id).style.backgroundColor=color;
  27.   }
  28. </script>
  29. <style type="text/css">
  30. body,td,th {
  31. color: #000;
  32. }
  33. body {
  34. }
  35. </style>
  36. </head>
  37.  
  38. <body>
  39. <form action="reportlima.php" method="get" >
  40.  
  41. <table BGCOLOR="F9FBFC" BORDERCOLOR="EAEAEB"  BORDER="1" width="900" align="center" cellpadding="1" >
  42. <tr>
  43.   <td colspan=6 align="center" BGCOLOR="C1D4D6" style="color:#000000"><h2><strong>Consulta</strong></h2></td>
  44.  
  45. </tr>
  46.  
  47. <TR>
  48. <TD align=left><B>Fecha Notificacion RI</B></TD>
  49.  
  50. <TR>
  51. <TD align=right><B> Desde:</B></TD>
  52. <TD align=left> <input type="text" name="fecha_reporte" id="fecha_reporte" style="background-color:#FBF9D5"/>
  53. <button type="submit" id="button1">....</button>
  54. <script type="text/javascript">           Calendar.setup({
  55.               inputField    : "fecha_reporte",
  56.               button        : "button1",
  57.               align         : "center"
  58.             });
  59.                 </script>
  60. </TD></TR>
  61.  
  62. <TR>
  63. <TD align=right><B> Hasta:</B></TD>
  64. <TD align=left> <input type="text" name="fecha_reporte2" id="fecha_reporte2" style="background-color:#FBF9D5"/>
  65.  
  66. <button type="submit" id="button2">....</button>
  67. <script type="text/javascript">           Calendar.setup({
  68.               inputField    : "fecha_reporte2",
  69.               button        : "button2",
  70.               align         : "center"
  71.             });
  72.                 </script>
  73. </TD></TR>
  74.  <TR>
  75.   <TD colspan=6>
  76.    <br/>
  77.  
  78. <center>  
  79. <input type="submit" name="busc" value="Buscar" style='width:100px; height:35px'>
  80. <input type="button" name="reg" value="Regresar" style='width:100px; height:35px' onClick="history.back()"></center>
  81.  
  82. </table>
  83. </form>
  84. <br></br>
  85. <table width="1450"  align="center" cellspacing="0">
  86. <TD align=left><input type="submit" name="exporta" value="Exportar a Excel" style='width:150px; height:35px'></TD>
  87.  
  88. </table>
  89. <table BGCOLOR="F9FBFC" BORDERCOLOR=""  BORDER=5 width="1450"  align="center" cellspacing="0">
  90. <tr>
  91. <td colspan=14 align="center" bgcolor="#FFFFFF"><h2><strong></strong></h2></td>
  92.  </tr>
  93. <tr>
  94.   <td BGCOLOR="C1D4D6" width="25" align=center><h5>N &#38;#176;</h5></td>
  95.   <td BGCOLOR="C1D4D6" width="90" align=center><h5>DEPENDENCIA</h5></td>
  96.   <td BGCOLOR="C1D4D6" width="85" align=center><b><h5>RUC</h5></b></td>
  97.   <td BGCOLOR="C1D4D6" width="150" align=center><h5>N CUENTA</h5></td>
  98.   <td BGCOLOR="C1D4D6" width="230" align=center><h5>RAZON SOCIAL</h5></td>
  99.   <td BGCOLOR="C1D4D6" width="100" align=center><h5>CODIGO RESLUCION</h5></td>
  100.   <td BGCOLOR="C1D4D6" width="100" align=center><h5>FECHA NOTIFICACION</h5></td>
  101. <td BGCOLOR="C1D4D6" width="330" align=center><h5>CAUSAL DE INGRESO</BR>DERECAUDACION</h5></td>
  102.   <td BGCOLOR="C1D4D6" width="100" align=center><h5>TIPO MONTO<BR> A TRASLADAR</h5></td>
  103.   <td BGCOLOR="C1D4D6" width="90" align=center><h5>OBSERVACIONES</h5></td>
  104.   <td BGCOLOR="C1D4D6" width="90" align=center><h5>PERIODOS DE REVISION</h5></td>
  105.   <td BGCOLOR="C1D4D6" width="90" align=center><h5>MONTO A TRASLADAR</h5></td>
  106.   <td BGCOLOR="C1D4D6" width="70" align=center><h5>ESTADO</h5></td>
  107.  
  108. </tr>
  109. <?php
  110. $i=0;
  111. while($res=mysql_fetch_array($reg)){
  112. ?>
  113.   <tr style="background-color:#f0f0f0" id="<?php echo $i;?>" onMouseMove="cambiar('<?php echo $i;?>','#cccccc')" onMouseOut="cambiar('<?php echo $i;?>','#f0f0f0')">
  114.  
  115.  <td width="25" align=center><?php echo $res[''];?></td>
  116.    <td width="90" align=center>00<?php echo $res['icr_dependencia'];?></td>
  117.    <td width="85" align=center><?php echo $res['icr_ruc'];?></td>
  118.    <td width="150" align=center><?php echo $res['icr_cuenta_bn'];?></td>
  119.    <td width="230" align=justify><?php echo $res['icr_contribuyente'];?></td>
  120.    <td width="100" align=center>0<?php echo $res['icr_dependencia'];?>024000<?php echo $res['icr_num_resolucion'];?></td>
  121.    <td width="100" align=center><?php echo $res['icr_fecha_ri'];?></td>
  122.    <td width="330" align=justify><?php echo $res['icr_causal'];?></td>
  123.    <td width="90" align=center>
  124. <?php if($res["icr_traslado"]==1){?>Saldo Parcial
  125. <?php }elseif($res["icr_traslado"]==2){?>Saldo Total
  126. <?php }?> </td>
  127. <td width="90" align=justify><?php echo $res[''];?></td>
  128.  
  129.  
  130.  
  131. <!---  AQUI ES DONDE SE DEBE AGREGAR EL CODIGO
  132.  
  133. <td width="25" align=center><?php echo $res['icr_fecha_revision1'];?></td>
  134.  
  135. si lo hago de esta manera me  sale asi 2015-12-24
  136. Pero lo que quiero es que salga Diciembre del 2015
  137. --->
  138.  
  139. <td width="90" align=center><?php echo $res['icr_monto_icr'];?></td>
  140.  
  141.  <td width="70" align=center>
  142. <?php if($res["icr_estado"]==1){?>Registrado
  143. <?php }elseif($res["icr_estado"]==2){?>Con Comunicaci&oacute;n
  144. <?php }elseif($res["icr_estado"]==3){?>Notificaci&#38;#243n Comunicaci&#38;#243n
  145. <?php }elseif($res["icr_estado"]==4){?>Con Resoluci&oacute;n
  146. <?php }elseif($res["icr_estado"]==5){?>Notificado con RI
  147. <?php }elseif($res["icr_estado"]==6){?>Reportado
  148. <?php }?>
  149. </td>
  150.  
  151. </tr>
  152. <?php
  153. $i=$i+1;
  154. }
  155. ?>
  156. </table>
  157. </body>
  158.  
  159. </html>
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  


Título: Re: PHP Cambiar fecha cadena a date
Publicado por: 0roch1 en 2 Diciembre 2015, 17:29 pm
Esta es una forma de hacerlo.

Código
  1. <?php
  2. $fecha = "2015-10-21";
  3.  
  4. $format = format_date($fecha);
  5. echo $format;
  6.  
  7. function format_date($fecha) {
  8.  
  9. $array_meses = array('Enero' => 1, 'Febrero' => 2, 'Marzo' => 3, 'Julio' => 7, 'Octubre' => 10, 'Diciembre' => 12);
  10.  
  11. if(!empty($fecha)) {
  12. list($año, $mes, $dia) = explode("-", $fecha);
  13. if(checkdate($mes, $dia, $año)) {
  14. $nombre_mes = array_search($mes, $array_meses);
  15. return $nombre_mes.' del '.$año;
  16. }else {
  17. return "Fecha no valida";
  18. }
  19. }
  20. }
  21. ?>
  22.  


Título: Re: PHP Cambiar fecha cadena a date
Publicado por: gAb1 en 2 Diciembre 2015, 18:52 pm
No es más facil hacer simplemente:

Código
  1. echo date('F \d\e\l Y', strtotime('2015-12'));

Y esto es lo que impirimiria:

Citar
Diciembre del 2015

Tendrias que quitarle el día o ponerlo tambien:

Código
  1. echo date('l\, j \d\e F \d\e Y', strtotime('2015-12-24'));

Leete el manual (http://php.net/manual/es/function.date.php) y sabrás como darle formato a las fechas. Y te recomiendo almacenar las fechas en formato unix (como enteros) ya que luego simplemente puedes hacer:

Código
  1. $time = 1450944000; // esto es lo que sacas de la db
  2. echo date('l\, j \d\e F \d\e Y', $time);

Y lo que imprime:

Citar
Jueves, 24 de Diciembre del 2015


Título: Re: PHP Cambiar fecha cadena a date
Publicado por: tomasvreal28 en 4 Diciembre 2015, 04:10 am
amigo pero tu lo estas haciendo con un tipo date
 
y yo lo tengo en un varchar

y están guardados de esta manera  Año-Mes-Dia   2015-12-31

y lo que quiero es que se muestre Mes y año  para mostrarlo en un listado
por ejemplo Enero del 2015 
pero extrayendolo de  mi bd   el campo se llama icr_fecha_revision2

Código
  1. <?php
  2. require_once('conexion.php');
  3. if (isset($_POST['busc'])){
  4. $fecha_reporte=$_POST['fecha_reporte'];
  5. $fecha_reporte2=$_POST['fecha_reporte2'];
  6.  
  7. $sql="select * from tbldocumentosicr INNER JOIN tblinfracciones ON tbldocumentosicr.icr_infraccion=tblinfracciones.icr_infraccion where icr_fecha_ri>='$fecha_reporte' and icr_fecha_ri<='$fecha_reporte2' order by icr_fecha_ri ASC";
  8. }else{
  9. $sql="select * from tbldocumentosicr INNER JOIN tblinfracciones ON tbldocumentosicr.icr_infraccion=tblinfracciones.icr_infraccion where icr_estado='5' order by icr_fecha_ri ASC";
  10. }
  11.  
  12. $reg=mysql_query($sql,$cnx);
  13. ?>
  14. <html>
  15. <head>
  16. <title>Listado</title>
  17. </head>
  18. <body>
  19. <form action="reportlima.php" method="POST" >
  20. <table BGCOLOR="F9FBFC" BORDERCOLOR=""  BORDER=5 width="100%"  align="center" cellspacing="0">
  21. <tr>
  22. <td colspan=14 align="center" bgcolor="#FFFFFF"><h2><strong></strong></h2></td>
  23. </tr>
  24. <tr>
  25.  
  26. <td BGCOLOR="C1D4D6" width="90" align=center><h5>PERIODOS DE REVISION</h5></td>
  27.  
  28. </tr>
  29. <?php
  30. $i=0;
  31. while($res=mysql_fetch_array($reg)){
  32. ?>
  33. <tr style="background-color:#f0f0f0" id="<?php echo $i;?>" onMouseMove="cambiar('<?php echo $i;?>','#cccccc')" onMouseOut="cambiar('<?php echo $i;?>','#f0f0f0')">
  34.  
  35.  
  36. <!--- [b]AQUI ES DONDE DEBERIA AGREGAR EL CODIGO[/b] --->
  37.  
  38. // ASI ES COMO LO EXTRAIGO DE LA BD
  39.  
  40. <td width="90" align=center><?php echo $res['icr_fecha_revision2'];?></td>
  41.  
  42. --->
  43.  
  44. </tr>
  45. <?php
  46. $i=$i+1;
  47. }
  48. ?>
  49. </table>
  50. </body>
  51. </html>
  52.  


Título: Re: PHP Cambiar fecha cadena a date
Publicado por: gAb1 en 4 Diciembre 2015, 16:48 pm
No veo el problema de usar date() (http://php.net/manual/es/function.date.php) para lo que necesitas, si te fijas verás que tambien uso strtotime() (http://php.net/manual/es/function.strtotime.php). Si quieres imprimir el nombre del mes debes o bien usar date() que lo hace automatico o usar la función de @0roch1:

Código
  1. $i=0;
  2. while($res=mysql_fetch_array($reg)){
  3. ?>
  4. <tr style="background-color:#f0f0f0" id="<?php echo $i;?>" onMouseMove="cambiar('<?php echo $i;?>','#cccccc')" onMouseOut="cambiar('<?php echo $i;?>','#f0f0f0')">
  5.  
  6. <!---  AQUI ES DONDE DEBERIA AGREGAR EL CODIGO --->
  7. <!---  ASI ES COMO LO EXTRAIGO DE LA BD --->
  8. $data = explode('-', $res['icr_fecha_revision2']);
  9.  
  10. <td width="90" align=center><?php echo date('F \d\e\l Y', strtotime($data[0] . '-' . $data[1])); ?></td>
  11.  
  12. </tr>
  13. <?php
  14. $i=$i+1;
  15. }
  16. ?>

Pruebalo y verás como funciona  :)


Título: Re: PHP Cambiar fecha cadena a date
Publicado por: tomasvreal28 en 5 Diciembre 2015, 03:54 am
Muchas Gracias funciona
pero sale en ingles el mes como haría para que salga en español
porque lo estoy tratando de hacerlo por medio de un array pero no se como agregarlo



Código
  1. <?php
  2.  
  3. date_default_timezone_set('America/Lima');
  4.  
  5. $calendario_meses = array(1=>"Enero",2=>"Febrero",3=>"Marzo",4=>"Abril",5=>"Mayo",6=>"Junio",
  6. 7=>"Julio",8=>"Agosto",9=>"Septiembre",10=>"Octubre",
  7. 11=>"Noviembre",12=>"Diciembre");
  8.  
  9. ?>
  10.  
  11. <?php
  12.    $data = explode('-', $res['icr_fecha_revision2']);
  13.    echo date('F \d\e\l Y', strtotime($data[0] . '-' . $data[1])); ?>
  14. </td>
  15.  
  16.