本文实例讲述了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
|
<?php
date_default_timezone_set( 'Asia/Shanghai' );
echo "今天:" . date ( "Y-m-d H:i:s" ). "<br>" ;
echo "昨天:" . date ( "Y-m-d" , strtotime ( "-1 day" )), "<br>" ;
echo "明天:" . date ( "Y-m-d" , strtotime ( "+1 day" )). "<br>" ;
echo "一周后:" . date ( "Y-m-d" , strtotime ( "+1 week" )). "<br>" ;
echo "一周前:" . date ( "Y-m-d" , strtotime ( "-1 week" )). "<br>" ;
echo "一周零两天四小时两秒后:" . date ( "Y-m-d G:H:s" , strtotime ( "+1 week 2 days 4 hours 2 seconds" )). "<br>" ;
echo "下个星期四:" . date ( "Y-m-d" , strtotime ( "next Thursday" )). "<br>" ;
echo "上个周一:" . date ( "Y-m-d" , strtotime ( "last Monday" )). "<br>" ;
echo "一个月前:" . date ( "Y-m-d" , strtotime ( "last month" )). "<br>" ;
echo "一个月后:" . date ( "Y-m-d" , strtotime ( "+1 month" )). "<br>" ;
echo "十年后:" . date ( "Y-m-d" , strtotime ( "+10 year" )). "<br>" ;
echo '<hr/>' ;
//php获取今日开始时间戳和结束时间戳
$beginToday = mktime (0,0,0, date ( 'm' ), date ( 'd' ), date ( 'Y' ));
$endToday = mktime (0,0,0, date ( 'm' ), date ( 'd' )+1, date ( 'Y' ))-1;
echo "今日开始时间戳和结束时间戳" , '开始:' , $beginToday , '结束:' , $endToday , '<br/>' ;
echo "今日开始时间:" , date ( "Y-m-d H:i:s" , $beginToday ), '<br/>' ;
echo "今日结束时间:" , date ( "Y-m-d H:i:s" , $endToday ), '<br/>' ;
echo '<hr/>' ;
//php获取昨日起始时间戳和结束时间戳
$beginYesterday = mktime (0,0,0, date ( 'm' ), date ( 'd' )-1, date ( 'Y' ));
$endYesterday = mktime (0,0,0, date ( 'm' ), date ( 'd' ), date ( 'Y' ))-1;
echo "昨日开始时间戳和结束时间戳" , '开始:' , $beginYesterday , '结束:' , $endYesterday , '<br/>' ;
echo "昨日开始时间:" , date ( "Y-m-d H:i:s" , $beginYesterday ), '<br/>' ;
echo "昨日结束时间:" , date ( "Y-m-d H:i:s" , $endYesterday ), '<br/>' ;
echo '<hr/>' ;
//php获取上周起始时间戳和结束时间戳
$beginLastweek = mktime (0,0,0, date ( 'm' ), date ( 'd' )- date ( 'w' )+1-7, date ( 'Y' ));
$endLastweek = mktime (23,59,59, date ( 'm' ), date ( 'd' )- date ( 'w' )+7-7, date ( 'Y' ));
echo "上周开始时间戳和结束时间戳" , '开始:' , $beginLastweek , '结束:' , $endLastweek , '<br/>' ;
echo "上周开始时间:" , date ( "Y-m-d H:i:s" , $beginLastweek ), '<br/>' ;
echo "上周结束时间:" , date ( "Y-m-d H:i:s" , $endLastweek ), '<br/>' ;
echo '<hr/>' ;
//php获取本月起始时间戳和结束时间戳
$beginThismonth = mktime (0,0,0, date ( 'm' ),1, date ( 'Y' ));
$endThismonth = mktime (23,59,59, date ( 'm' ), date ( 't' ), date ( 'Y' ));
echo "本月开始时间戳和结束时间戳" , '开始:' , $beginThismonth , '结束:' , $endThismonth , '<br/>' ;
echo "本月开始时间:" , date ( "Y-m-d H:i:s" , $beginThismonth ), '<br/>' ;
echo "本月结束时间:" , date ( "Y-m-d H:i:s" , $endThismonth ), '<br/>' ;
?>
|
运行结果:
今天:2019-07-06 10:23:11
昨天:2019-07-05
明天:2019-07-07
一周后:2019-07-13
一周前:2019-06-29
一周零两天四小时两秒后:2019-07-15 14:14:13
下个星期四:2019-07-11
上个周一:2019-07-01
一个月前:2019-06-06
一个月后:2019-08-06
十年后:2029-07-06--------------------------------------------------------------------------------
今日开始时间戳和结束时间戳开始:1562342400结束:1562428799
今日开始时间:2019-07-06 00:00:00
今日结束时间:2019-07-06 23:59:59--------------------------------------------------------------------------------
昨日开始时间戳和结束时间戳开始:1562256000结束:1562342399
昨日开始时间:2019-07-05 00:00:00
昨日结束时间:2019-07-05 23:59:59--------------------------------------------------------------------------------
上周开始时间戳和结束时间戳开始:1561305600结束:1561910399
上周开始时间:2019-06-24 00:00:00
上周结束时间:2019-06-30 23:59:59--------------------------------------------------------------------------------
本月开始时间戳和结束时间戳开始:1561910400结束:1564588799
本月开始时间:2019-07-01 00:00:00
本月结束时间:2019-07-31 23:59:59
希望本文所述对大家PHP程序设计有所帮助。
原文链接:https://blog.csdn.net/helin916012530/article/details/25082957