3

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);
?>
DMSJax
  • 1,709
  • 4
  • 22
  • 35

4 Answers4

15

wont this work?

echo date("d m Y",strtotime('monday this week'));
Mithun Satheesh
  • 27,240
  • 14
  • 77
  • 101
  • 4
    `'Monday this week'` also works in the `DateTime` constructor if you want something more modern than `strtotime()` – Phil Jul 16 '13 at 05:04
  • 1
    Close, 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 "
    ".date("m/d/Y",$test);`
    – DMSJax Jul 16 '13 at 05:10
  • 1
    @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? – RN Kushwaha Jan 30 '15 at 07:30
  • 1
    @RNKushwaha - this works based on "a week starts on Sunday". you can read the threads on [the docs](http://php.net/manual/en/function.strtotime.php). – Mithun Satheesh Jan 31 '15 at 10:32
0

You Can try This :


                     date_default_timezone_set("Asia/Kolkata"); 
                    $day = date("w")."<br>";
                    echo $day;
                    //if( $day == 1 ) {$day = 0;}
                    if( $day == 2 ) {$day -= 1;}
                    if( $day == 3 ) {$day -= 1;}
                    if( $day == 4 ) {$day -= 1;}
                    if( $day == 5 ) {$day -= 1;}
                    if( $day == 6 ) {$day -= 1;}
                    if( $day == 0 ) {$day -= 6;}
                    $calc = mktime(0,0,0,date("m"),date("d")-$day,date("Y"));
                            if($day == 1 ){
                                    echo date("m/d/Y")."<br>";
                                    echo "start date of work week is: ".date("m/d/Y");                                      
                            }   
                            else{
                                    echo date("m/d/Y",$calc)."<br>";
                                    echo "start date of work week is: ".date("m/d/Y", $calc);                                      

                            }
0
$time = strtotime('monday this week');
josliber
  • 43,891
  • 12
  • 98
  • 133
Joel
  • 49
  • 7
0

The following works if your week starts on Monday.

$monday = strtotime('this sunday', strtotime($startingDate)) - (86400 * 6);

While if you do it the way @mithunsatheesh suggested works perfectly if your week starts on Sunday.

Pablo Santamaria
  • 727
  • 1
  • 8
  • 20