PHP Script: get a year of dates starting a certain number of days from a given date
This script is called get-a-year-of-dates-starting-a-certain-number-of-days-from-a-given-date.php
In searching the Internet, we found no evidence of a PHP script illustrating getting a year's worth of dates into an array, starting from a certain date or starting from a certain amount of days from a given date. In the script below, we do just that. To get a year's worth of dates into an array, starting from a certain date, make $d=0 in the second line and change the date in the first line to the date to start from. To get a year's worth of dates into an array, starting from a certain amount of days from a given date, make $d=the number of days from the given date to begin the year (in the second line) and change the date in the first line to the date to start from.
The PHP DateTime class has the methods we need to manipulate dates as needed. The DateTime::__construct method returns a new DateTime object. The DateTime::add method adds an amount of days, months, years, hours, minutes and seconds to a DateTime object. And the DateTime::format method returns a date formatted according to given format.
In the script, in the for loop put in 364 if you want a year of dates in the array, 6 in place of 364 if you want a week, 29 in place of 364 if you want 30 days—you get the idea. And you replace our sample date 02/19/2011 with your chosen starting date. And replace $d=7 with $d=0 if you want the array to start on the given date, or $d=365 if you want it to start in a year—you get the idea.
We define a $dates() array, then create a new DateTime object with the date we stick in $dates[0]. The inserted date used slash separators, but we format it for hyphens for the array. We use the date_add() method on the date along with the date_interval_create_from_date_string() function to add days to the date we created. In the example, we wanted the year of dates (that will go in the array) to begin a week after our sample date 02/19/2011, so we put 7 in $d. But as soon as the for loop is started, $d gets a 1 and it stays a 1. This increments the $date object by 1 per loop after that initial 1-week offset. Every loop, the date, formatted with hyphen separators, gets dumped into the $dates() array. There are plenty of other things you can choose to do with the script below as a starting place, or with using the
DateTime objects and methods here in scripts you conjure up yourself. There is a JavaScript script inside Edit Scheduler that does similar date handling, and a PHP version in Replace This Week With Next Week that uses the array values to update a MySQL table.
This script is called get-a-year-of-dates-starting-a-certain-number-of-days-from-a-given-date.php
<?php
//get a year of dates starting a certain number of days from a given date
$dates=array();$dates[0]="02/19/2011";
$da=$dates[0];$d=7;
$date = new DateTime($da);
$da= $date->format('m-d-Y');
for($i = 0; $i <= 364; $i++){
date_add($date, date_interval_create_from_date_string($d.' days'));
$datee= $date->format('m-d-Y');
$dates[$i]=$datee;$d=1;
echo $dates[$i]."<BR>";}
?>