thanks - thats useful - it does give me the dates until Jan 2013 which stops me having to look in my diary!
I’m not sure how I then pick out the ‘next’ one.
Also what controls how far into the future it goes … i.e. why does it go to Jan 2013 and not stop at the end of 2012 - or for that matter go to feb 16th 2013 as that would be a full 12 months? And does it ‘move forward’ automatically as the current date progresses?
It was showing Date from Jan 2013 because of simple bug. I fixed it. See the new code.
// initialize date to current date
$dt = time();
while(true){
// go to next months first date
$dt = mktime(date("H", $dt), date("i", $dt), date("s", $dt), date("n", $dt)+1, 1);
$dt = strtotime("+3 Saturday", $dt);
if(date("Y", $dt) == 2013)
break;
echo date(DATE_RSS, $dt). "\
";
};
If you want to get next 12 months date then count a variable in the loop. When it reaches to 12 break loop.
Yes, It moves forward automatically. I initialize the date to current date. This is the starting point. You can choose your starting point.
Thanks too cups - but no - its the 3rd saturday not the last (phew).
That works a little better for me - I have ‘some’ more control.
See below for my updated version of your script.
It works fine until I request the 12th month from now - then it returns this year’s ‘12 month date’ not next years 12 month date (-ie 18th Feb 2012 not 16th Feb 2013). Not sure why…
// initialize date to current date
$dt = time();
$monthcount=0; //pd start a month count at zero
$tdcount=0; //pd start a table cell count at zero
while(true){
// go to next months first date
$dt = mktime(date("H", $dt), date("i", $dt), date("s", $dt), date("n", $dt)+1, 1);
$dt = strtotime("+3 Saturday", $dt);
++$monthcount; //pd add 1 to the month count
++$tdcount; //pd add 1 to the table cell count
if ($monthcount >= 13) //stop getting the dates after 12 months
break;
//echo $monthcount; //pd print month count for checking purposes
if ($tdcount > 6) { //if the number of dates is greater than 12 insert a new table row
echo "</tr><tr>";
$tdcount=0;//reset the table cell count
}
//echo date(DATE_RSS, $dt). "\
";
echo "<td>" . date("d-F y", $dt). "</td>";
};
Most of the problem with that code is it’s only incrementing the month… and ONLY the month, so it can’t roll over the date properly. The answer is to leverage strtotime’s text parsing TWICE… once for “third saturday of this month” and once for “next month” after… It might also be helpful to add a trap so that a date from this month isn’t added to the list, and I’m gonna wrap it in a function here so you can pass a request for any number of months returned as an array.
<?php
function getThirdSaturdays($months) {
$baseStamp=$now=time();
$result=array();
while ($months>0) {
$baseStamp=strtotime('third saturday of this month',$baseStamp);
if ($baseStamp>$now) {
$result[]=$baseStamp;
$months--;
}
$baseStamp=strtotime('next month',$baseStamp);
}
return $result;
}
echo '
<ul>';
$thirdSaturdayList=getThirdSaturdays(36); // three YEARS to test rollover
foreach ($thirdSaturdayList as $thirdSaturday) {
echo '
<li>',date('j F Y',$thirdSaturday),'</li>';
}
echo '
</ul>';
?>
Should work on everything right up to the unix gateway of 2038… and past that if you’re on a system upgraded to do time as 64 bit instead of 32.
Just thought about it, lets get that if statement out of the loop.
function getThirdSaturdays($months) {
$baseStamp=(
strtotime('third saturday of this month')>time() ?
time() :
strtotime('last month')
);
$result=array();
while ($months-- >0) {
$result[]=($baseStamp=strtotime('third saturday of next month',$baseStamp));
}
return $result;
}
programming 101, anything you can figure out before the loop, should go before the loop instead of inside it.
I can see yours works perfectly but I’ve copied and pasted from thirdSaturdays.phps and I get a set of dates all reading
1 January 1970. I wonder if my server is failing to pick up the server time / date in the first instance.