Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Computing relative dates in php using strtotime()

Tags:

php

strtotime

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

like image 603
jalperin Avatar asked Jun 13 '12 19:06

jalperin


2 Answers

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

This searches for the Monday previous to the next Sunday.

like image 141
Jeff Jenkins Avatar answered Nov 07 '22 13:11

Jeff Jenkins


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.

like image 21
Stu Avatar answered Nov 07 '22 14:11

Stu