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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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>";

运行结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
当前: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

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :