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
--->
<?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ón
<?php }elseif($res["icr_estado"]==3){?>Notificación Comunicación
<?php }elseif($res["icr_estado"]==4){?>Con Resolució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>
Esta es una forma de hacerlo.
<?php
$fecha = "2015-10-21";
$format = format_date($fecha);
echo $format;
function format_date($fecha) {
$array_meses = array('Enero' => 1, 'Febrero' => 2, 'Marzo' => 3, 'Julio' => 7, 'Octubre' => 10, 'Diciembre' => 12);
if(!empty($fecha)) {
list($año, $mes, $dia) = explode("-", $fecha);
if(checkdate($mes, $dia, $año)) {
$nombre_mes = array_search($mes, $array_meses);
return $nombre_mes.' del '.$año;
}else {
return "Fecha no valida";
}
}
}
?>
No es más facil hacer simplemente:
echo date('F \d\e\l Y', strtotime('2015-12'));
Y esto es lo que impirimiria:
CitarDiciembre del 2015
Tendrias que quitarle el día o ponerlo tambien:
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:
$time = 1450944000; // esto es lo que sacas de la db
echo date('l\, j \d\e F \d\e Y', $time);
Y lo que imprime:
CitarJueves, 24 de Diciembre del 2015
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
<?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>
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:
$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')">
<!--- AQUI ES DONDE DEBERIA AGREGAR EL CODIGO --->
<!--- ASI ES COMO LO EXTRAIGO DE LA BD --->
$data = explode('-', $res['icr_fecha_revision2']);
<td width="90" align=center><?php echo date('F \d\e\l Y', strtotime($data[0] . '-' . $data[1])); ?></td>
</tr>
<?php
$i=$i+1;
}
?>
Pruebalo y verás como funciona :)
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
<?php
date_default_timezone_set('America/Lima');
$calendario_meses = array(1=>"Enero",2=>"Febrero",3=>"Marzo",4=>"Abril",5=>"Mayo",6=>"Junio",
7=>"Julio",8=>"Agosto",9=>"Septiembre",10=>"Octubre",
11=>"Noviembre",12=>"Diciembre");
?>
<?php
$data = explode('-', $res['icr_fecha_revision2']);
echo date('F \d\e\l Y', strtotime($data[0] . '-' . $data[1])); ?>
</td>