PHP获取今天、昨天、前三天、本周、上周、本月、上个月、上个季度、本季度、本年时间戳、上一年时间戳

cfn
2020-12-01
707

函数讲解

mktime($hour, $minute, $second, $month, $day, $year, $is_dst) : int

(PHP 4, PHP 5, PHP 7)

mktime — 取得一个日期的 Unix 时间戳

参数 介绍
hour 小时数
minute 分钟数
second 秒数(一分钟之内)
month 月份数
day 天数
year 年份数
is_dst PHP 7.0.0 起,此参数已经被移除。

strtotime (string $datetime, int $now = time()) : int

(PHP 4, PHP 5, PHP 7)

strtotime — 将任何字符串的日期时间描述解析为 Unix 时间戳

今天时间戳

$beginTime = mktime(0,0,0,date("m"),date("d"),date("Y")); $endTime = mktime(0,0,0,date("m"),date("d")+1,date("Y"))-1; var_dump($beginTime,$endTime); # int(1606752000) # int(1606838399)

昨日时间戳

$beginTime = mktime(0,0,0,date("m"),date("d")-1,date("Y")); $endTime = mktime(0,0,0,date("m"),date("d"),date("Y"))-1; var_dump($beginTime,$endTime); # int(1606665600) # int(1606751999)

前三天时间戳

$beginTime = mktime(0,0,0,date('m'),date('d') - 3, date('Y')); $endTime = mktime(23,59,59,date('m'),date('d') - 1, date('Y')); var_dump($beginTime,$endTime); # int(1606579200) # int(1606838399)

本周时间戳

$beginTime = mktime(0,0,0,date('m'),date('d') - date('w')+1, date('Y')); $endTime = mktime(23,59,59,date('m'),date('d') - date('w')+7, date('Y')); var_dump($beginTime,$endTime); # int(1606665600) # int(1607270399)

上周时间戳

$beginTime = mktime(0,0,0,date('m'),date('d') - date('w') + 1 - 7, date('Y')); $endTime = mktime(23,59,59,date('m'),date('d') - date('w') + 7 - 7, date('Y')); var_dump($beginTime,$endTime); # int(1607270400) # int(1607875199)

本月时间戳

$beginTime = mktime(0,0,0, date('m'),1, date('Y')); $endTime = mktime(23,59,59, date('m'), date('t'), date('Y')); var_dump($beginTime,$endTime); # int(1606752000) # int(1609430399)

上个月时间戳

$beginTime = strtotime(date('Y-m-01 00:00:00', strtotime('-1 month'))); $endTime = strtotime(date("Y-m-d 23:59:59", strtotime(-date('d').'day'))); var_dump($beginTime,$endTime); # int(1604160000) # int(1606751999)

本季度时间戳

$season = ceil((date('n'))/3); $beginTime = mktime(0, 0, 0, $season * 3 - 3 + 1, 1, date('Y')); $endTime = mktime(23, 59, 59, $season * 3, date('t', mktime(0, 0 , 0, $season * 3, 1, date("Y"))), date('Y')); var_dump($beginTime,$endTime); # int(1601481600) # int(1609430399)

上个季度时间戳

$season = ceil((date('n'))/3) - 1; $beginTime = mktime(0, 0, 0, $season * 3 - 3 + 1, 1, date('Y')); $endTime = mktime(23, 59, 59, $season * 3, date('t', mktime(0, 0 , 0, $season * 3, 1, date("Y"))), date('Y')); var_dump($beginTime,$endTime); # int(1593532800) # int(1601481599)

本年时间戳

$beginTime = mktime(0, 0, 0, 1, 1, date('Y')); $endTime = mktime(23, 59, 59, 12, 31, date('Y')); var_dump($beginTime,$endTime); # int(1577808000) # int(1609430399)

上一年时间戳

$beginTime = mktime(0, 0, 0, 1, 1, date('Y') - 1); $endTime = mktime(23, 59, 59, 12, 31, date('Y') - 1); var_dump($beginTime,$endTime); # int(1546272000) # int(1577807999)
评论 1