Calculate date for Monday of current week
11,382
wont this work?
echo date("d m Y",strtotime('monday this week'));
Related videos on Youtube
Author by
DMSJax
Updated on September 18, 2022Comments
-
DMSJax about 1 year
Goal: If current day of week is any day other than Monday, display the date of the Monday of the current week. If the current day of the week is Monday, simply display today's date.
** This is what I wrote and I think it works but is probably not the cleanest way to determine the date. Having said that, does anyone see any reason why the code would be wrong or not work? **
<?php date_default_timezone_set("America/New_York"); $day = date("w"); if( $day == 1 ) {$day -= 0;} if( $day == 2 ) {$day -= 1;} if( $day == 3 ) {$day -= 2;} if( $day == 4 ) {$day -= 3;} if( $day == 5 ) {$day -= 4;} if( $day == 6 ) {$day -= 5;} if( $day == 0 ) {$day -= 6;} $calc = mktime(0,0,0,date("m"),date("d")-$day,date("Y")); echo date("m/d/Y",$calc)."<br>"; echo "start date of work week is: ".date("m/d/Y", $calc); ?>
-
Phil over 10 years
'Monday this week'
also works in theDateTime
constructor if you want something more modern thanstrtotime()
-
DMSJax over 10 yearsClose, doing it that way echoed the upcoming Monday instead of the Monday in the current week. I was however able to look at the
strtotime()
method and get the right date by using$test=strtotime("previous monday",strtotime(date("m/d/Y"))); echo "<br>".date("m/d/Y",$test);
-
RN Kushwaha almost 9 years@mithunsatheesh This provides wrong information in my case. If date is 1st jan 2015 then it provides 2 jan 2015 as monday of current month while week should count from monday. I there a way to count current week start from monday rather than sunday?
-
Mithun Satheesh almost 9 years@RNKushwaha - this works based on "a week starts on Sunday". you can read the threads on the docs.