php获取天周月季度的开始结束时间戳
首页 >
php
作者:knight
2019年11月4日 7:58 星期一
热度:455°
百度已收录
时间:2019-11-4 7:58
热度:455°
/**
*
*/
class Test
{
/**
* 获取今天本周本月的开始结束时间戳
* @Author zkl
* @DateTime 2019-11-04
* @copyright [copyright]
* @license [license]
* @version [version]
* @param string $type [day,week,month,quarter]
* @param string $time [时间]
* @return [type] [description]
*/
public static function getTime($type='day',$time='')
{
if (!$time) {
$time = time();
}else{
//判断是否为时间戳
$re = self::is_timestamp($time);
if (!$re) {
//不是时间戳,判断是否为日期格式,返回对应时间戳
$time = strtotime($time)?strtotime($time):time();
}
}
$result = [];
switch ($type) {
case 'day':
//获取今天开始结束时间
$dayStart = strtotime(date("Y-m-d",$time));
$dayEnd = strtotime(date("Y-m-d 23:59:59",$time));
$result = [$dayStart,$dayEnd];
break;
case 'week':
$w=date('w');
//获取本周开始日期,如果$w是0,则表示周日,减去 6 天
$first=1;
//周一
$week = date('Y-m-d H:i:s',strtotime( date("Ymd",$time)."-".($w ? $w - $first : 6).' days'));
$week_start = strtotime( date("Ymd",$time)."-".($w ? $w - $first : 6).' days');
//本周结束日期
//周天
$week_end = strtotime("{$week} +1 week")-1;
$result = [$week_start,$week_end];
break;
case 'month':
//本月第一天
$month = date('Y-m-d',strtotime(date("Ym",$time).'01'));
$month_start = strtotime(date("Ym",$time).'01');
//本月最后一天
$month_end = strtotime("{$month} +1 month")-1;
$result = [$month_start,$month_end];
break;
case 'quarter':
$season = ceil(date('n',$time) /3);
$firstday = strtotime(date('Y-m-01',mktime(0,0,0,($season - 1) *3 +1,1,date('Y'))));
$lastday = strtotime(date('Y-m-t',mktime(0,0,0,$season * 3,1,date('Y'))));
$result = [$firstday,$lastday];
break;
default:
//获取今天开始结束时间
$dayStart = strtotime(date("Y-m-d",$time));
$dayEnd = strtotime(date("Y-m-d 23:59:59",$time));
$result = [$dayStart,$dayEnd];
break;
}
return $result;
}
public function is_timestamp($timestamp) {
if(strtotime(date('Y-m-d H:i:s', $timestamp)) === $timestamp) {
return $timestamp;
} else {
return false;
}
}
}
var_dump(Test::getTime('week',1551683190));
昵称
邮件地址 (选填)
个人主页 (选填)