/    Sign up×
Community /Pin to ProfileBookmark

Find Last Occurrence of Date?

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.

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJan 16.2011 — Best I could think of on the spur of the moment:
[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]

Output:
<i>
</i>2010-09-30
2010-09-30
2009-09-30
2009-09-30
×

Success!

Help @msmith29063 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...