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
This is what I am using to get the first and last day of the week from any date. In this case, monday is the first day of the week...
Here's a one liner for the first day of last week, and the last day of last week as a
DateTime
object.A smart way of doing this is to let PHP handle timezone differences and Daylight Savings Time (DST). Let me show you how to do this.
This function will generate all days from Monday until Friday, inclusive (handy for generating work week days):
This will return datetimes Monday-Friday for current week. To do the same for an arbitrary date, pass a date as a parameter to
DateTimeUtilities ::getPeriodFromMondayUntilFriday
, thus:Only interested in Monday, as the OP asked?
This question needs a good DateTime answer:-
Output:-
This will deal with year boundaries and leap years.
For what it's worth, here is a breakdown of the wonky behavior of strtotime when determining a consistent frame of reference:
http://gamereplays.org/reference/strtotime.php
Basically only these strings will reliably give you the same date, no matter what day of the week you're currently on when you call them: