首页  >  文章  >  后端开发  >  获取各时间段的时间戳,上月、当月、上周、当周、上日、今日

获取各时间段的时间戳,上月、当月、上周、当周、上日、今日

Yang
Yang原创
2020-07-20 16:43:12114浏览

/**
* 获取时间段的开始、结束时间戳
* @param string $timetype 时间类型 yesmonth-上月 nowmonth-当月 yesday-上一天 nowday-今日 yesweek-上周 nowweek-上周
*/
function getFLtime($timetype){
switch ($timetype){
case 'yesmonth':
//计算上一月
$first = date('Y-m-d H:i:s', mktime(0, 0, 0, date('m') - 1, 1, date('Y')));
$last = date('Y-m-d 23:59:59', strtotime("$first +1 month -1 day"));
break;
case 'nowmonth':
$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 'yesday':
$first = date('Y-m-d 0:0:0',time()-3600*24);
$last = date('Y-m-d 23:59:59',strtotime($first));
break;
case 'nowday':
$first = date('Y-m-d 0:0:0',time());
$last = date('Y-m-d 23:59:59',strtotime($first));
break;
case 'yesweek':
$timestamp = time();
$first = date('Y-m-d H:i:s', strtotime("last week Monday", $timestamp));
$last = date('Y-m-d H:i:s', (strtotime(date('Y-m-d', strtotime("last week Sunday", $timestamp))) + 24 * 3600 - 1));
break;
case 'nowweek':
$timestamp = time();
$first = date('Y-m-d H:i:s', strtotime("this week Monday", $timestamp));
$last = date('Y-m-d H:i:s', (strtotime(date('Y-m-d H:i:s', strtotime("this week Sunday", $timestamp))) + 24 * 3600 - 1));
break;
default:
return false;
break;
}
return ['start'=>strtotime($first),'end'=>strtotime($last)];
}

?>

以上是获取各时间段的时间戳,上月、当月、上周、当周、上日、今日的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn