There has got to be a better/simpler solution for this than what I came up with. How can I use PHP to find the date for the last occurrence of September 30th? I’m then going to use it in a MySQL search query. Thanks for your help.
[code=php] <?php /** * Get the most recent September 30th as a UNIX timestamp * @return int * @param int $now (optional timestamp for testing) */ function getLastSep30($now=null) { if(!$now) { $now = time(); } $yearNow = date('Y', $now); $monthNow = date('n', $now); $lastSep30ts = ($monthNow > 9) ? mktime(0,0,0,9,30,$yearNow) : mktime(0,0,0,9,30,$yearNow - 1); return$lastSep30ts; } // normal usage echo date('Y-m-d', getLastSep30()) . "<br />n"; // tests of boundary conditions: echo date('Y-m-d', getLastSep30(strtotime('2010-10-01'))) . "<br />n"; echo date('Y-m-d', getLastSep30(strtotime('2010-09-30'))) . "<br />n"; echo date('Y-m-d', getLastSep30(strtotime('2010-09-29'))); [/code]
<i> </i>2010-09-30 2010-09-30 2009-09-30 2009-09-30
Help @msmith29063 spread the word by sharing this article on Twitter...