6

I'm looking for a reliable way to return the full date of a specified weekday (e.g. "Mon") for the current week.

Since today is Wednesday, June 13, 2012, I expected <?php echo date("Y-m-d", strtotime('Mon this week')); ?> to result in 2012-06-11, but instead php returns 2012-06-18 as though it interprets this week as meaning next week. Why this behavior and what should I be doing?

Thanks.

--Jeff

jalperin
  • 2,664
  • 9
  • 30
  • 32
  • 4
    How do you define "this week"? Monday though Sunday? Sunday through Saturday? Saturday through Friday? Or something else completely? – Ben Lee Jun 13 '12 at 19:59
  • 1
    interesting. I just ran it under PHP5.4, and got the 11th for 'Mon this week' and the 18th for 'this Mon'. What version PHP are you running? – dnagirl Jun 13 '12 at 20:00
  • 1
    also, I assumed that a "week" started either on Sunday or Monday since the date() formats give options for both – jalperin Jun 13 '12 at 20:03
  • 3
    How about ``? – j08691 Jun 13 '12 at 20:03
  • 1
    does it give the same wrong date when you use a DateTime object? e.g. `$d=new DateTime('Mon this week'); echo $d->format('Y-m-d');` – dnagirl Jun 13 '12 at 20:11

3 Answers3

6

date( 'Y-m-d', strtotime( 'last Monday', strtotime( 'Sunday' ) ) );

This searches for the Monday previous to the next Sunday.

jeffjenx
  • 17,041
  • 6
  • 57
  • 99
3

According to the documentation php relative date formats.

Then Monday this week would first advance to the next Monday and then process the relative text of this week.

dayname: Moves to the next day of this name unless it is the current day then it will not advance. In other words if the current date was June 11, then strtotime('Monday this week') would return June 11 whereas if the current date was June 13 then strtotime('Monday this week') would return June 19.

Stu
  • 340
  • 2
  • 8
0

i think this is the solution for your problem:

$monday_date = date("Y-m-d", mktime(0,0,0, date("m"), date("j")-(date("w")+1), date("Y")));
silly
  • 7,789
  • 2
  • 24
  • 37