Foros del Web » Programando para Internet » PHP »

PHP OO PHP Cambiar fecha cadena a date

Estas en el tema de PHP Cambiar fecha cadena a date en el foro de PHP en Foros del Web. 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 ...
  #1 (permalink)  
Antiguo 01/12/2015, 19:17
 
Fecha de Ingreso: noviembre-2015
Mensajes: 21
Antigüedad: 8 años, 5 meses
Puntos: 0
PHP Cambiar fecha cadena a date

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 PHP:
<?php
require_once('conexion.php');

if (isset(
$_GET['busc'])){
$fecha_reporte=$_GET['fecha_reporte'];
$fecha_reporte2=$_GET['fecha_reporte2'];
$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";
}else{
$sql="select * from tbldocumentosicr INNER JOIN tblinfracciones ON tbldocumentosicr.icr_infraccion=tblinfracciones.icr_infraccion where icr_estado='5' order by icr_fecha_ri ASC";
}
$reg=mysql_query($sql,$cnx);
?>
<html>
<head>
<title>Listado</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <script type="text/javascript" src="calendar.js"></script>
    <script type="text/javascript" src="calendar-setup.js"></script>
    <script type="text/javascript" src="calendar-es.js"></script>
    <style type="text/css"> @import url("calendar-win2k-cold-1.css"); </style>

<script type="text/javascript" language="javascript">
   function cambiar(id,color){
       document.getElementById(id).style.backgroundColor=color;
   }
</script>
<style type="text/css">
body,td,th {
   color: #000;
}
body {
}
</style>
</head>

<body>
<form action="reportlima.php" method="get" >

<table BGCOLOR="F9FBFC" BORDERCOLOR="EAEAEB"  BORDER="1" width="900" align="center" cellpadding="1" >
<tr>
   <td colspan=6 align="center" BGCOLOR="C1D4D6" style="color:#000000"><h2><strong>Consulta</strong></h2></td>

</tr>

<TR>
<TD align=left><B>Fecha Notificacion RI</B></TD> 

<TR>
<TD align=right><B> Desde:</B></TD>
<TD align=left> <input type="text" name="fecha_reporte" id="fecha_reporte" style="background-color:#FBF9D5"/>
<button type="submit" id="button1">....</button>
<script type="text/javascript">                Calendar.setup({
                                inputField    : "fecha_reporte",
                                button        : "button1",
                                align         : "center"
                              });
                               </script>
</TD></TR>

<TR>
<TD align=right><B> Hasta:</B></TD>
<TD align=left> <input type="text" name="fecha_reporte2" id="fecha_reporte2" style="background-color:#FBF9D5"/>

<button type="submit" id="button2">....</button>
<script type="text/javascript">                Calendar.setup({
                                inputField    : "fecha_reporte2",
                                button        : "button2",
                                align         : "center"
                              });
                               </script>
</TD></TR>
  <TR> 
   <TD colspan=6>
    <br/>

 <center>   
 <input type="submit" name="busc" value="Buscar" style='width:100px; height:35px'>
<input type="button" name="reg" value="Regresar" style='width:100px; height:35px' onClick="history.back()"></center>

 </table>
</form>
<br></br>
<table width="1450"  align="center" cellspacing="0">
<TD align=left><input type="submit" name="exporta" value="Exportar a Excel" style='width:150px; height:35px'></TD>

</table>
<table BGCOLOR="F9FBFC" BORDERCOLOR=""  BORDER=5 width="1450"  align="center" cellspacing="0">
<tr>
 <td colspan=14 align="center" bgcolor="#FFFFFF"><h2><strong></strong></h2></td>
  </tr>
<tr>
   <td BGCOLOR="C1D4D6" width="25" align=center><h5>N °</h5></td>
   <td BGCOLOR="C1D4D6" width="90" align=center><h5>DEPENDENCIA</h5></td>
   <td BGCOLOR="C1D4D6" width="85" align=center><b><h5>RUC</h5></b></td>
   <td BGCOLOR="C1D4D6" width="150" align=center><h5>N CUENTA</h5></td>
   <td BGCOLOR="C1D4D6" width="230" align=center><h5>RAZON SOCIAL</h5></td>
   <td BGCOLOR="C1D4D6" width="100" align=center><h5>CODIGO RESLUCION</h5></td>
   <td BGCOLOR="C1D4D6" width="100" align=center><h5>FECHA NOTIFICACION</h5></td>
 <td BGCOLOR="C1D4D6" width="330" align=center><h5>CAUSAL DE INGRESO</BR>DERECAUDACION</h5></td>
   <td BGCOLOR="C1D4D6" width="100" align=center><h5>TIPO MONTO<BR> A TRASLADAR</h5></td>
   <td BGCOLOR="C1D4D6" width="90" align=center><h5>OBSERVACIONES</h5></td>
   <td BGCOLOR="C1D4D6" width="90" align=center><h5>PERIODOS DE REVISION</h5></td> 
   <td BGCOLOR="C1D4D6" width="90" align=center><h5>MONTO A TRASLADAR</h5></td>
   <td BGCOLOR="C1D4D6" width="70" align=center><h5>ESTADO</h5></td>
  
 </tr>
<?php
$i
=0;
while(
$res=mysql_fetch_array($reg)){
   
?>
   <tr style="background-color:#f0f0f0" id="<?php echo $i;?>" onMouseMove="cambiar('<?php echo $i;?>','#cccccc')" onMouseOut="cambiar('<?php echo $i;?>','#f0f0f0')">

  <td width="25" align=center><?php echo $res[''];?></td>
    <td width="90" align=center>00<?php echo $res['icr_dependencia'];?></td>
    <td width="85" align=center><?php echo $res['icr_ruc'];?></td>
    <td width="150" align=center><?php echo $res['icr_cuenta_bn'];?></td>
    <td width="230" align=justify><?php echo $res['icr_contribuyente'];?></td>
    <td width="100" align=center>0<?php echo $res['icr_dependencia'];?>024000<?php echo $res['icr_num_resolucion'];?></td>
    <td width="100" align=center><?php echo $res['icr_fecha_ri'];?></td>
    <td width="330" align=justify><?php echo $res['icr_causal'];?></td>
    <td width="90" align=center>
<?php if($res["icr_traslado"]==1){?>Saldo Parcial
<?php }elseif($res["icr_traslado"]==2){?>Saldo Total
<?php }?>   </td>
<td width="90" align=justify><?php echo $res[''];?></td>



<!---  AQUI ES DONDE SE DEBE AGREGAR 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
--->

<td width="90" align=center><?php echo $res['icr_monto_icr'];?></td>
 
  <td width="70" align=center>
<?php if($res["icr_estado"]==1){?>Registrado
<?php }elseif($res["icr_estado"]==2){?>Con Comunicaci&oacute;n
<?php }elseif($res["icr_estado"]==3){?>Notificación Comunicación
<?php }elseif($res["icr_estado"]==4){?>Con Resoluci&oacute;n
<?php }elseif($res["icr_estado"]==5){?>Notificado con RI
<?php }elseif($res["icr_estado"]==6){?>Reportado
<?php }?>
   </td>

</tr>
<?php
$i
=$i+1;
}
?>
</table>
</body>

</html>
  #2 (permalink)  
Antiguo 02/12/2015, 09:02
Avatar de hhs
hhs
Colaborador
 
Fecha de Ingreso: junio-2013
Ubicación: México
Mensajes: 2.995
Antigüedad: 10 años, 9 meses
Puntos: 379
Respuesta: PHP Cambiar fecha cadena a date

Es muy sencillo, utiliza la extensión intl: http://php.net/manual/es/book.intl.php te dejo un ejemplo como base, utilizando la fecha que tienes en tu mensaje.
Código PHP:
Ver original
  1. $fmt = new IntlDateFormatter(
  2.     "es_ES" ,
  3.     IntlDateFormatter::FULL,
  4.     IntlDateFormatter::FULL,
  5.     null,
  6.     IntlDateFormatter::GREGORIAN,
  7.     "MMMM 'de' yyyy"
  8. );
  9.  
  10. $date = new DateTime('2015-12-24');
  11.  
  12. echo utf8_decode($fmt->format($date)); //Imprime diciembre de 2015
__________________
Saludos
About me
Laraveles
A class should have only one reason to change.
  #3 (permalink)  
Antiguo 03/12/2015, 21:15
 
Fecha de Ingreso: noviembre-2015
Mensajes: 21
Antigüedad: 8 años, 5 meses
Puntos: 0
Respuesta: PHP Cambiar fecha cadena a date

amigo pero tu lo estas haciendo con un tipo date y ademas agregando una fecha determinada

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 PHP:
<?php
require_once('conexion.php');
if (isset(
$_POST['busc'])){
$fecha_reporte=$_POST['fecha_reporte'];
$fecha_reporte2=$_POST['fecha_reporte2'];

$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";
}else{
$sql="select * from tbldocumentosicr INNER JOIN tblinfracciones ON tbldocumentosicr.icr_infraccion=tblinfracciones.icr_infraccion where icr_estado='5' order by icr_fecha_ri ASC";
}

$reg=mysql_query($sql,$cnx);
?>
<html>
<head>
<title>Listado</title>
</head>
<body>
<form action="reportlima.php" method="POST" >
<table BGCOLOR="F9FBFC" BORDERCOLOR=""  BORDER=5 width="100%"  align="center" cellspacing="0">
<tr>
<td colspan=14 align="center" bgcolor="#FFFFFF"><h2><strong></strong></h2></td>
</tr>
<tr>

<td BGCOLOR="C1D4D6" width="90" align=center><h5>PERIODOS DE REVISION</h5></td> 

</tr>
<?php
$i
=0;
while(
$res=mysql_fetch_array($reg)){
    
?>
<tr style="background-color:#f0f0f0" id="<?php echo $i;?>" onMouseMove="cambiar('<?php echo $i;?>','#cccccc')" onMouseOut="cambiar('<?php echo $i;?>','#f0f0f0')">
    

<!--- [b]AQUI ES DONDE DEBERIA AGREGAR EL CODIGO[/b] --->

// ASI ES COMO LO EXTRAIGO DE LA BD

<td width="90" align=center><?php echo $res['icr_fecha_revision2'];?></td>

--->

</tr>
    <?php
    $i
=$i+1;
    }
    
?>
</table>
</body>
</html>
  #4 (permalink)  
Antiguo 04/12/2015, 17:21
 
Fecha de Ingreso: diciembre-2015
Ubicación: México
Mensajes: 280
Antigüedad: 8 años, 4 meses
Puntos: 20
Respuesta: PHP Cambiar fecha cadena a date

Hola amigo, hay una manera más simple, (eso creo :p )
Lo que puedes hacer es procesar tu cadena
Yo lo haria de ésta forma:

Código PHP:
Ver original
  1. $fecha = $res['icr_fecha_revision1'];
  2. $items = explode('-',$fecha); //Divide el string en un array, partiendo el string acuerdo al caracter indicado, en este caso '-'
Ahora en tu arreglo $items ya tienes tus elementos:

Código PHP:
Ver original
  1. $año = $items[0];
  2. $mes = $items[1];
  3. $dia   = $items[2];

Ahora ya puedes dar el formato que quieres, con un if switch o algún otro método, eso te lo dejo a ti :p

Etiquetas: cadena, date, fecha, html, registro, select, sql
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 06:00.