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 gives you the day of the week of the given date itself where
0
= Sunday and6
= Saturday. From there you can simply calculate backwards to the day you want.This is the shortest and most readable solution I found:
strtotime
is faster thannew DateTime()->getTimestamp()
.(Note: MM, dd and yyyy in the Question are not standard php date format syntax - I can't be sure what is meant, so I set the $start_date with ISO year-month-day)
Here is what I am using...
$day contains a number from 0 to 6 representing the day of the week (Sunday = 0, Monday = 1, etc.).
$week_start contains the date for Sunday of the current week as mm-dd-yyyy.
$week_end contains the date for the Saturday of the current week as mm-dd-yyyy.
If you want Monday as the start of your week, do this:
What about:
Its not the best way but i tested and if i am in this week i get the correct monday, and if i am on a monday i will get that monday.