PHP获取当天、当月、当年的起点与终点时间戳

PHP获取当天、当月、当年的起点与终点时间戳

date_default_timezone_set("Asia/Shanghai");
$time = time();

$day_start = mktime(0,0,0,date("m",$time),date("d",$time),date("Y",$time));
$day_end = mktime(23,59,59,date("m",$time),date("d",$time),date("Y",$time));
$day_pre1_start = mktime(0,0,0,date("m",$time),date("d",$time)-1,date("Y",$time));
$day_pre1_end = mktime(23,59,59,date("m",$time),date("d",$time)-1,date("Y",$time));
$day_next1_start = mktime(0,0,0,date("m",$time),date("d",$time)+1,date("Y",$time));
$day_next1_end = mktime(23,59,59,date("m",$time),date("d",$time)+1,date("Y",$time));
$day_pre2_start = mktime(0,0,0,date("m",$time),date("d",$time)-2,date("Y",$time));
$day_pre2_end = mktime(23,59,59,date("m",$time),date("d",$time)-2,date("Y",$time));
$day_next2_start = mktime(0,0,0,date("m",$time),date("d",$time)+2,date("Y",$time));
$day_next2_end = mktime(23,59,59,date("m",$time),date("d",$time)+2,date("Y",$time));

$month_start = mktime(0,0,0,date("m",$time),1,date("Y",$time));
$month_end = mktime(23,59,59,date("m",$time),date("t"),date("Y",$time));
$month_pre1_start = mktime(0,0,0,date("m",$time)-1,1,date("Y",$time));
$month_pre1_end = mktime(23,59,59,date("m",$time)-1,date("t",$month_pre1_start),date("Y",$time));
$month_next1_start = mktime(0,0,0,date("m",$time)+1,1,date("Y",$time));
$month_next1_end = mktime(23,59,59,date("m",$time)+1,date("t",$month_next1_start),date("Y",$time));

$year_start = mktime(0,0,0,1,1,date("Y",$time));
$year_end = mktime(23,59,59,12,31,date("Y",$time));
$year_pre1_start = mktime(0,0,0,1,1,date("Y",$time)-1);
$year_pre1_end = mktime(23,59,59,12,31,date("Y",$time)-1);
$year_next1_start = mktime(0,0,0,1,1,date("Y",$time)+1);
$year_next1_end = mktime(23,59,59,12,31,date("Y",$time)+1);

//测试

echo date("当前:Y-m-d H:i:s", $time) . " $time<br><br>";

echo date("今天起点:Y-m-d H:i:s", $day_start) . " $day_start<br>";
echo date("今天终点:Y-m-d H:i:s", $day_end) . " $day_end<br>";
echo date("昨天起点:Y-m-d H:i:s", $day_pre1_start) . " $day_pre1_start<br>";
echo date("昨天终点:Y-m-d H:i:s", $day_pre1_end) . " $day_pre1_end<br>";
echo date("明天起点:Y-m-d H:i:s", $day_next1_start) . " $day_next1_start<br>";
echo date("明天终点:Y-m-d H:i:s", $day_next1_end) . " $day_next1_end<br>";
echo date("前天起点:Y-m-d H:i:s", $day_pre2_start) . " $day_pre2_start<br>";
echo date("前天终点:Y-m-d H:i:s", $day_pre2_end) . " $day_pre2_end<br>";
echo date("后天起点:Y-m-d H:i:s", $day_next2_start) . " $day_next2_start<br>";
echo date("后天终点:Y-m-d H:i:s", $day_next2_end) . " $day_next2_end<br><br>";

echo date("今月起点:Y-m-d H:i:s", $month_start) . " $month_start<br>";
echo date("今月终点:Y-m-d H:i:s", $month_end) . " $month_end<br>";
echo date("上月起点:Y-m-d H:i:s", $month_pre1_start) . " $month_pre1_start<br>";
echo date("上月终点:Y-m-d H:i:s", $month_pre1_end) . " $month_pre1_end<br>";
echo date("下月起点:Y-m-d H:i:s", $month_next1_start) . " $month_next1_start<br>";
echo date("下月终点:Y-m-d H:i:s", $month_next1_end) . " $month_next1_end<br><br>";

echo date("今年起点:Y-m-d H:i:s", $year_start) . " $year_start<br>";
echo date("今年终点:Y-m-d H:i:s", $year_end) . " $year_end<br>";
echo date("去年起点:Y-m-d H:i:s", $year_pre1_start) . " $year_pre1_start<br>";
echo date("去年终点:Y-m-d H:i:s", $year_pre1_end) . " $year_pre1_end<br>";
echo date("明年起点:Y-m-d H:i:s", $year_next1_start) . " $year_next1_start<br>";
echo date("明年终点:Y-m-d H:i:s", $year_next1_end) . " $year_next1_end<br>";

运行结果:

当前:2018-01-27 17:55:16 1517046916

今天起点:2018-01-27 00:00:00 1516982400
今天终点:2018-01-27 23:59:59 1517068799
昨天起点:2018-01-26 00:00:00 1516896000
昨天终点:2018-01-26 23:59:59 1516982399
明天起点:2018-01-28 00:00:00 1517068800
明天终点:2018-01-28 23:59:59 1517155199
前天起点:2018-01-25 00:00:00 1516809600
前天终点:2018-01-25 23:59:59 1516895999
后天起点:2018-01-29 00:00:00 1517155200
后天终点:2018-01-29 23:59:59 1517241599

今月起点:2018-01-01 00:00:00 1514736000
今月终点:2018-01-31 23:59:59 1517414399
上月起点:2017-12-01 00:00:00 1512057600
上月终点:2017-12-31 23:59:59 1514735999
下月起点:2018-02-01 00:00:00 1517414400
下月终点:2018-02-28 23:59:59 1519833599

今年起点:2018-01-01 00:00:00 1514736000
今年终点:2018-12-31 23:59:59 1546271999
去年起点:2017-01-01 00:00:00 1483200000
去年终点:2017-12-31 23:59:59 1514735999
明年起点:2019-01-01 00:00:00 1546272000
明年终点:2019-12-31 23:59:59 1577807999

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/02/18/php-obtain-the-starting-and-ending-timestamps-of-the-current-day-month-and-year/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
PHP获取当天、当月、当年的起点与终点时间戳
PHP获取当天、当月、当年的起点与终点时间戳 date_default_timezone_set("Asia/Shanghai"); $time = time(); $day_start = mktime(0,0,0,date(&qu……
<<上一篇
下一篇>>
文章目录
关闭
目 录