DMSJax
DMSJax

Reputation: 1727

Calculate date for Monday of current week

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);
?>

Upvotes: 3

Views: 11719

Answers (4)

Pablo Santamaria
Pablo Santamaria

Reputation: 767

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.

Upvotes: 0

Joel
Joel

Reputation: 59

$time = strtotime('monday this week');

Upvotes: 0

Swapnil Chaudhari
Swapnil Chaudhari

Reputation: 75

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); }

Upvotes: 0

Mithun Satheesh
Mithun Satheesh

Reputation: 27845

wont this work?

echo date("d m Y",strtotime('monday this week'));

Upvotes: 15

Related Questions