通过php查找一周的第一天

Fut*_*ing 13 php datetime date

可能重复:
获取PHP中的第一天?

嗨,

我想查找本周和上周的第一个和最后一个日期.同样,我想查找当月和上个月的第一个和最后一个日期.

这必须在PHP中完成.请帮忙.

Fel*_*ing 43

strtotime相对时间格式相当强大:

strtotime('monday this week');
strtotime('sunday this week');
strtotime('monday last week');
strtotime('sunday last week');
Run Code Online (Sandbox Code Playgroud)

(这仅适用于PHP 5.3+)

strtotime('first day of this month');
strtotime('last day of this month');
strtotime('first day of last month');
strtotime('last day of last month');
Run Code Online (Sandbox Code Playgroud)

为了获得在PHP <5.3一个月的第一个和最后一个日期,你可以使用的组合mktimedate(date('t')给出了一个月的天数):

mktime(0,0,0,null, 1); // gives first day of current month
mktime(0,0,0,null, date('t')); // gives last day of current month

$lastMonth = strtotime('last month');
mktime(0,0,0,date('n', $lastMonth), 1); // gives first day of last month
mktime(0,0,0,date('n', $lastMonth), date('t', $lastMonth); // gives last day of last month
Run Code Online (Sandbox Code Playgroud)

如果您只想获得一个字符串进行演示,那么您不需要mktime:

date('Y-m-1'); // first day current month
date('Y-m-t'); // last day current month
date('Y-m-1', strtotime('last month')); // first day last month
date('Y-m-t', strtotime('last month')); // last day last month
Run Code Online (Sandbox Code Playgroud)