Reputation: 719
I'm working with php timestamps to develop a calendar of events page.
On the Calendar page, I've written a function to determine a few things right away such as:
$first_of_month = strtotime(date('m',time()).'/01/'.date('Y',time()));
Now - using the the first day of the month, I must determine the most recent Sunday prior to that day (the Calendar starts on Sundays), so I run it through this:
date('m/d/Y--D-w',($first_of_month - (date('w',$first_of_month)*86400)))
the problem is it's not returning what I would expect.
Currently we're in April, 2016. Day 1 of April returns 5 when run through the date('w') format. So, 5 days back from that Friday should be day 0 (Sunday). But I get Saturday.
What am I missing here?
Upvotes: 2
Views: 66
Reputation: 6319
PHPs date word parser is extremely powerful. If you're looking for the last Sunday of last month, you can just ask!
$date = new DateTime();
if($date->format('w') != 0) {
$date->modify('last Sunday of last month');
}
See ideone here: https://ideone.com/sWsxPW
Upvotes: 1
Reputation: 132
Does the 1 hour offset put you past midnight (into tomorrow)? I'd recommend you double check your server offset: date_default_timezone_get();
If it is wrong, use date_default_timezone_set('America/Los_Angeles')
(obviously replacing 'America/Los_Angeles' as appropriate. php: date_default_timezone_set()
As long as that's correct you could get the last Sunday by using strtotime() on $first_of_month
:
date('m/d/Y--D-w' ,strtotime('last Sunday',$first_of_month));
Upvotes: 1