I am trying to create a function to create an array of DateTime(s). The dates I want are every Saturday in a given year.
I have a function that does this but when the values are stored in an array, they begin with the second Saturday of the year and extend to the first Saturday of the following year.
Note that the following displays a list of Saturdays as generated by the function and then after a couple of blank lines displays the Saturdays store in the array.
<?php
define ('sql','Y-m-d');
define ('br','<br/>');
function allSaturdays ($year){
$endofyear = "$year-12-31";
$interval = new DateInterval("P7D");
$year--;
$workingdate = "$year-12-31";
$workingdate = strtotime ($workingdate);
$workingdate = strtotime ("next Saturday",$workingdate);
$workingdate = date ("Y-m-d",$workingdate);
$workingdate = new DateTime ($workingdate);
$result[] = new DateTime;
while ($workingdate->format(sql) <= $endofyear ) {
$result[] = $workingdate;
echo $workingdate->format (sql).br;
$workingdate->add ($interval);
$workingdate = new DateTime ($workingdate->format(sql));
//echo $workingdate->format (sql)."#".br;;
}// while
unset ($workingdate);
return $result;
}//function
$sats = allSaturdays(2016);
echo "<br/.<br/>";
foreach ($sats as $saturday)
echo $saturday->format(sql)."*<br>";
?>
However, if I store the only the date (2016-01-02) the correct values are in the array.
No doubt I'm missing something simple. Any help is appreciated.
Dave