Quantcast
Channel: Only a Bill
Viewing all articles
Browse latest Browse all 2

Using PHP To Generate The Dates Between Two Dates

$
0
0

I recently had to populate a database table with all the possible dates between two given dates, which is a reasonably common thing to do. I did it in command-line php and thought the code might be helpful to someone else, so here it is:

#!/usr/local/bin/php -q
<?
 
$start_date = "01-jan-2012";
$end_date = "01-jan-2022";
 
$time = strtotime($date);
 
while ($time < strtotime($end_date)) {
   $dow = date("w", $time);
   $date_format_1 = date("d-M-Y", $time);
   $date_format_2 = date("Ymd", $time);
   $month = date("m", $time);
   $year = date("Y", $time);
   $month_shortname = date("M", $time);
   $day_shortname  = date("D", $time);
   $month_longname = date("F", $time);
   $day_longname = date("l", $time);
 
   echo "$dow $date_format_1 $date_format_2 $month $year $month_longname $month_shortname $day_longname $day_shortname\n";
 
   // change this line to include the database insert of your choice
 
   $time = strtotime("+1 day", $time);
}
 
?>

And there you go.


Viewing all articles
Browse latest Browse all 2

Trending Articles