Fel*_*ing 45
$first_minute = mktime(0, 0, 0, date("n"), 1);
$last_minute = mktime(23, 59, 59, date("n"), date("t"));
Run Code Online (Sandbox Code Playgroud)
这是本月.如果您想在任何月份使用它,则相应地更改月份和日期参数.
如果你想每个月生成它,你可以循环:
$times = array();
for($month = 1; $month <= 12; $month++) {
$first_minute = mktime(0, 0, 0, $month, 1);
$last_minute = mktime(23, 59, 59, $month, date('t', $first_minute));
$times[$month] = array($first_minute, $last_minute);
}
Run Code Online (Sandbox Code Playgroud)
eno*_*rev 15
使用PHP 5.3,你可以做到
$oFirst = new DateTime('first day of this month');
$oLast = new DateTime('last day of this month');
$oLast->setTime(23, 59, 59);
Run Code Online (Sandbox Code Playgroud)
在PHP 5.2中
注意:正如AllThecode在下面的评论中指出的那样,下一个示例仅在您$oFirst首先执行该部分时才有效.如果你添加+1 month到new DateTime结果将在该月的最后一天提前一个月跳(从PHP 5.5.9开始).
$oToday = new DateTime();
$iTime = mktime(0, 0, 0, $oToday->format('m'), 1, $oToday->format('Y'));
$oFirst = new DateTime(date('r', $iTime));
$oLast = clone $oFirst;
$oLast->modify('+1 month');
$oLast->modify('-1 day');
$oLast->setTime(23, 59, 59);
Run Code Online (Sandbox Code Playgroud)