【发布时间】:2010-12-14 22:35:42
【问题描述】:
嗨,
我想查找本周和上周的第一个和最后一个日期。 同样,我想找到当前月份和上个月的第一个和最后一个日期。
这必须在 PHP 中完成。请帮忙。
【问题讨论】:
嗨,
我想查找本周和上周的第一个和最后一个日期。 同样,我想找到当前月份和上个月的第一个和最后一个日期。
这必须在 PHP 中完成。请帮忙。
【问题讨论】:
strtotime 与relative time formats 相当强大:
strtotime('monday this week');
strtotime('sunday this week');
strtotime('monday last week');
strtotime('sunday last week');
(这只适用于 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');
为了在 PHP mktime 和 date 的组合(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
如果你只是想得到一个字符串进行演示,那么你不需要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
【讨论】:
这是一周的第一天和最后一天的函数:
function week_start_date($wk_num, $yr, $first = 1, $format = 'F d, Y')
{
$wk_ts = strtotime('+' . $wk_num . ' weeks', strtotime($yr . '0101'));
$mon_ts = strtotime('-' . date('w', $wk_ts) + $first . ' days', $wk_ts);
return date($format, $mon_ts);
}
$sStartDate = week_start_date($week_number, $year);
$sEndDate = date('F d, Y', strtotime('+6 days', strtotime($sStartDate)));
它可能也可以适应月份,但我想得到我的答案! :)
【讨论】: