Recorrer un rango de fechas mediante php.
Desarrollo, PHP April 8th, 2008
El amigo Montecristo, del canal de Linux, me preguntó como hacer en PHP para recorrer un rango de fechas. Como no hay una función nativa en php para eso, le hice un pequeño script que aquí muestro. Espero le sirva a alguien más:
#!/usr/bin/php -q
<?
//Configurar aqui //////////
//inicio
$year_start = 2007;
$month_start = 11;
$day_start = 1;
//fin
$year_end = 2008;
$month_end = 3;
$day_end = 10;
///////////////////////////////
if( ! checkdate($month_start, $day_start, $year_start)){
print "invalid start date";
exit;
}
if( ! checkdate($month_end, $day_end, $year_end)){
print "invalid end date";
exit;
}
//obtener el dia siguiente a la fecha final indicada
$day_end++;
if (! checkdate($month_end, $day_end, $year_end)){
$day_end = 1;
$month_end++;
if (! checkdate($month_end, $day_end, $year_end)){
$month_end = 1;
$year_end++;
}
}
//no editar abajo
$year = $year_start;
$month = $month_start;
$day = $day_start;
$finish = 0;
while ( ! $finish && checkdate($month, $day, $year)) {
$date = $month . '-' . $day . '-' . $year;
//////////////////////////////////////
//hacer algo con la fecha aqui
print $date; print "\n";
//////////////////////////////////////
$day++;
if (! checkdate($month, $day, $year)){
$day = 1;
$month++;
if (! checkdate($month, $day, $year)){
$month = 1;
$day = 1;
$year++;
}
}
if( $year == $year_end && $month == $month_end && $day == $day_end){
$finish = true;
}
}
?>
April 12th, 2008 at 2:54 pm
no entendí ni mádere como dice Catón, pero bueno, se nota que sabes jajajajja y respeto a la gente que sabe de estas coshas..jaj abyes
January 2nd, 2009 at 8:00 am
tampoco es la gran cosa comprender… explicó al principio q lo q se necesitaba
gracias por el pedazo de codigo, a alguien seguro le va a servir