• 0
  • 0

PHP时间相关函数

2020-07-02 755 0 admin 所属分类:PHP 记录

获取特定时间范围

// 返回时间范围  kind  today (今天) ,  yesterday (昨天) , month (本月),lastmonth(上月)
function time_range($kind) {
	if ($kind == 'today') {
		$begin = strtotime(date('Y-m-d', TIMESTAMP));
		$end   = TIMESTAMP;
	} else if ($kind == 'yesterday') {
		$end   = strtotime(date('Y-m-d', TIMESTAMP));
		$begin = $end - 86400;
	} else if ($kind == 'month') {
		$begin = mktime(0, 0, 0, date('n', TIMESTAMP), 1, date('Y', TIMESTAMP));
		$end   = TIMESTAMP;
	} else if ($kind == 'lastmonth') {
		$begin = strtotime(date('Y-m-01 00:00:00', strtotime('-1 month')));
		$end   = strtotime(date("Y-m-d 23:59:59", strtotime(-date('d') . 'day')));
	} else {
		$begin = $end = 0;
	}
	return [$begin,$end];
}

取两个时间戳时间间隔 单位 天

// 时间范围  两个时间戳的时间间隔
function time_distance($begin,$end) {
	if (!$begin || !$end) {
		return 0;
	}

	if (!is_numeric($begin)) {
		$begin = strtotime($begin);
	}

	if (!is_numeric($end)) {
		$end = strtotime($end);
	}

	return ceil(($end-$begin)/86400);
}


返回顶部