Computing relative dates in php using strtotime()

岁酱吖の 提交于 2020-02-02 03:19:05

问题


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


回答1:


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

This searches for the Monday previous to the next Sunday.




回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/11022384/computing-relative-dates-in-php-using-strtotime

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!