When the date is given, how to get the Monday date of that week in php

Possible duplicate:
Get the first day of the week in PHP?

When the date is given, I have to get the Monday date of this week.

When 2012-08-08 is set, it should return to 2012-08-06.

+4
source share
2 answers
function last_monday($date) { if (!is_numeric($date)) $date = strtotime($date); if (date('w', $date) == 1) return $date; else return strtotime( 'last monday', $date ); } echo date('m/d/y', last_monday('8/14/2012')); // 8/13/2012 (tuesday gives us the previous monday) echo date('m/d/y', last_monday('8/13/2012')); // 8/13/2012 (monday throws back that day) echo date('m/d/y', last_monday('8/12/2012')); // 8/06/2012 (sunday goes to previous week) 

try: http://codepad.org/rDAI4Scr

... or the option that will be resurrected the next day (Monday), and not the previous week, just add the line:

  elseif (date('w', $date) == 0) return strtotime( 'next monday', $date ); 

try: http://codepad.org/S2NhrU2Z

You can give it a timestamp or string, you will return the timestamp

Documentation

+9
source

You can easily create a timestamp using the strtotime function - it takes both a phrase like “last Monday” and a secondary parameter, which is a timestamp that you can easily make from the date you use mktime (note that inputs for a specific date Hour,Minute,Second,Month,Day,Year ).

 <?php $monday=strtotime("monday this week", mktime(0,0,0, 8, 8, 2012)); echo date("Ymd",$monday); // Output: 2012-08-06 ?> 

Change "last monday" in strtotime to "monday this week" and now it works fine.

+2
source

All Articles