PHP获取今天、昨天、本周、上周、本月、上月、本季度、上季度、今年、去年的开始和结束日期:
<?php
function getDates($date_name){
$this_time = time();
switch ($date_name){
case 'today': //今天
$first = date('Y-m-d H:i:s', mktime(0, 0, 0, date("m", $this_time), date("d", $this_time), date("Y", $this_time)));
$last = date('Y-m-d H:i:s', mktime(23, 59, 59, date("m", $this_time), date("d", $this_time), date("Y", $this_time)));
break;
case 'yesterday': //昨天
$first = date('Y-m-d H:i:s', mktime(0, 0, 0, date("m"), date("d", $this_time) - 1, date("Y", $this_time)));
$last = date('Y-m-d H:i:s', mktime(23, 59, 59, date("m"), date("d", $this_time) - 1, date("Y", $this_time)));
break;
case 'this_week': //本周
$first = date('Y-m-d H:i:s', strtotime("this week Monday", $this_time));
$last = date('Y-m-d H:i:s', (strtotime(date('Y-m-d H:i:s', strtotime("this week Sunday", $this_time))) + 24 * 3600 - 1));
break;
case 'last_week': //上周
$first = date('Y-m-d', strtotime("last week Monday", $this_time));
$last = date('Y-m-d H:i:s', (strtotime(date('Y-m-d', strtotime("last week Sunday", $this_time))) + 24 * 3600 - 1));
break;
case 'this_month': //本月
$date = date("Y-m-d");
// 本月第一天
$first = date('Y-m-01 0:0:0', strtotime($date));
// 本月最后一天
$last = date('Y-m-d 23:59:59', strtotime("$first +1 month -1 day"));
break;
case 'last_month': //上月
$first = date('Y-m-d H:i:s', mktime(0, 0, 0, date('m') - 1, 1, date('Y')));
$last = date('Y-m-d H:i:s', mktime(23, 59, 59, date('m') - 1, date('t', $first), date('Y')));
break;
case 'this_quarter': //当前季度
$season = ceil(date('n') / 3);
$first = date('Y-m-d H:i:s', mktime(0, 0, 0, ($season - 1) * 3 + 1, 1, date('Y')));
$last = date('Y-m-d H:i:s', mktime(0, 0, 0, $season * 3, 1, date('Y')));
break;
case 'last_quarter': //上个季度
$season = ceil(date('n') / 3);
$first = date('Y-m-d H:i:s', mktime(0, 0, 0, ($season - 2) * 3 + 1, 1, date('Y')));
$last = date('Y-m-d H:i:s', mktime(23, 59, 59, ($season - 1) * 3, 1, date('Y')));
break;
case 'this_year': //今年
$first = date('Y-m-d H:i:s', mktime(0, 0, 0, 1, 1, date('Y')));
$last = date('Y-m-d H:i:s', mktime(23, 59, 59, 12, 31, date('Y')));
break;
case 'last_year': //去年
$year = date('Y') - 1;
$first = date('Y-m-d H:i:s', mktime(0, 0, 0, 1, 1, $year));
$last = date('Y-m-d H:i:s', mktime(23, 59, 59, 12, 31, $year));
break;
}
return [$first,$last];//$first开始时间 $last结束时间
}
getDates('today');
?>
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注【森林君博客】!
如果文章对你有帮助,你可以点击右边的 $ 打赏小编哟!
打赏小编
- 支付宝扫一扫
- 微信扫一扫