Given a date MM-dd-yyyy
format, can someone help me get the first day of the week?
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
The following code should work with any custom date, just uses the desired date format.
I tested the code with PHP 5.2.17 Results:
Given PHP version pre 5.3 following function gives you a first day of the week of given date (in this case - Sunday, 2013-02-03):
I found this quite frustrating given that my timezone is Australian and that
strtotime()
hates UK dates.If the current day is a Sunday, then
strtotime("monday this week")
will return the day after.To overcome this:
Assuming Monday as the first day of the week, this works:
Just use
date($format, strtotime($date,' LAST SUNDAY + 1 DAY'));
The easiest way to get first day(Monday) of current week is:
where 604800 - is count of seconds in 1 week(60*60*24*7).
This code get next Monday and decrease it for 1 week. This code will work well in any day of week. Even if today is Monday.