php中关于日期时间的主要几个函数:
目录
strtotime() - 将英文文本日期时间解析为 Unix 时间戳
date - 格式化一个本地时间
date函数的主要功能是:以想要的时间格式获取系统当前的日期和时间。
date ( string $format [, int $timestamp ] ) : string
返回将整数 timestamp 按照给定的格式字串而产生的字符串。如果没有给出时间戳则使用本地当前时间。换句话说,timestamp 是可选的,默认值为 time()。
format参数常用的选项:
------- 常用的年月日时分秒 ---------------
Y - 年份的四位数表示
y - 年份的两位数表示
m - 月份的数字表示(从 01 到 12)
M - 月份的短文本表示(用三个字母表示)
d - 一个月中的第几天(从 01 到 31)
D - 星期几的文本表示(用三个字母表示)
h - 12 小时制,带前导零(01 到 12)
H - 24 小时制,带前导零(00 到 23)
g - 12 小时制,不带前导零(1 到 12)
G - 24 小时制,不带前导零(0 到 23)
i - 分,带前导零(00 到 59)
s - 秒,带前导零(00 到 59)
------- 不常用的 ---------------
a - 小写形式表示:am 或 pm
A - 大写形式表示:AM 或 PM
j - 一个月中的第几天,不带前导零(1 到 31)
l('L' 的小写形式)- 星期几的完整的文本表示
N - 星期几的 ISO-8601 数字格式表示(1表示Monday[星期一],7表示Sunday[星期日])
S - 一个月中的第几天的英语序数后缀(2 个字符:st、nd、rd 或 th。与 j 搭配使用)
w - 星期几的数字表示(0 表示 Sunday[星期日],6 表示 Saturday[星期六])
z - 一年中的第几天(从 0 到 365)
W - 用 ISO-8601 数字格式表示一年中的星期数字(每周从 Monday[星期一]开始)
F - 月份的完整的文本表示(January[一月份] 到 December[十二月份])
n - 月份的数字表示,不带前导零(1 到 12)
t - 给定月份中包含的天数
L - 是否是闰年(如果是闰年则为 1,否则为 0)
o - ISO-8601 标准下的年份数字
B - Swatch Internet Time(000 到 999)
u - 微秒(PHP 5.2.2 中新增的)
e - 时区标识符(例如:UTC、GMT、Atlantic/Azores)
I(i 的大写形式)- 日期是否是在夏令时(如果是夏令时则为 1,否则为 0)
O - 格林威治时间(GMT)的差值,单位是小时(实例:+0100)
P - 格林威治时间(GMT)的差值,单位是 hours:minutes(PHP 5.1.3 中新增的)
T - 时区的简写(实例:EST、MDT)
Z - 以秒为单位的时区偏移量。UTC 以西时区的偏移量为负数(-43200 到 50400)
c - ISO-8601 标准的日期(例如 2013-05-05T16:34:42+00:00)
r - RFC 2822 格式的日期(例如 Fri, 12 Apr 2013 12:01:05 +0200)
U - 自 Unix 纪元(January 1 1970 00:00:00 GMT)以来经过的秒数
时区问题:
如果服务器默认的时区是我们的北京时区(PRC)的话,那么我们获取到的日期和时间就是正确的,但是如果服务器的时区不是我们的北京时区(PRC)的话,我们获取到的日期或时间就会有偏差,这是时区的问题
如果你有权限修改php.ini配置的话,那么你就可以将php.ini里的date.timezone = ??? 改成date.timezone = PRC,然后重启Apache,这样,你获取到的日期时间就是我们的北京时间了。
如果你没有权限修改php.ini配置的话,你也可以这样获取:
<?php
date_default_timezone_set('PRC');//使用PHP的date函数获取时间之前,先将时区设置为北京时区
echo "当前时间为:".date('Y-m-d H:i:s');//获取当前时间
?>
microtime — 返回当前 Unix 时间戳和微秒数
microtime ([ bool $get_as_float ] ) : mixed
microtime() 当前 Unix 时间戳以及微秒数。
本函数仅在支持 gettimeofday() 系统调用的操作系统下可用。
如果调用时不带可选参数,本函数以 "msec sec" 的格式返回一个字符串,其中 sec 是自 Unix 纪元(0:00:00 January 1, 1970 GMT)起到现在的秒数,msec 是微秒部分。字符串的两部分都是以秒为单位返回的。
如果给出了 get_as_float 参数并且其值等价于 TRUE,microtime() 将返回一个浮点数。
$t1 = microtime(true); //1554345190.5521
time — 返回当前的 Unix 时间戳
time ( void ) : int
返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的秒数。
strtotime() - 将英文文本日期时间解析为 Unix 时间戳
strtotime("now"); //1548748383 2019-01-29 15:53:03
strtotime("15 October 1980"); //1473004800 2016-09-05 00:00:00
strtotime("+5 hours"); //1548766383 2019-01-29 20:53:03
strtotime("+1 week"); //1549353183 2019-02-05 15:53:03
strtotime("+1 week 3 days 7 hours 5 seconds"); //1549637588 2019-02-08 22:53:08
strtotime("next Monday"); //1549209600 2019-02-04 00:00:00
strtotime("last Sunday"); //1548518400 2019-01-27 00:00:00
mktime() -返回一个日期的 UNIX 时间戳
mktime(hour,minute,second,month,day,year,is_dst);
is_dst => 可选。如果时间在夏令时 (DST) 期间,则设置为 1,否则设置为 0,若未知则设置为 -1(默认)。
如果未知,PHP 会自己进行查找(可能产生意外的结果)。
注意:该参数在 PHP 5.1.0 中被废弃。取而代之使用的是新的时区处理特性
mktime(0,0,0,10,3,1975); //181497600
常用示例
1、获取本周&&上周起始时间戳和结束时间戳
$curr = date("Y-m-d");
$w=date('w');//获取当前周的第几天 周日是 0 周一到周六是1-6
$beginLastweek=strtotime("$curr -".($w ? $w-1 : $w-6).' days');//获取本周开始日期,如果$w是0是周日:-6天;其它:-1天
//本周
$beginThisWeek=date('Y-m-d 00:00:00',$beginLastweek);
$endThisWeek=date('Y-m-d 23:59:59',strtotime("$beginThisWeek +6 days"));
echo $beginThisWeek;echo "<hr>";echo $endThisWeek;echo "<hr>";
//上周
$beginLastWeek=date('Y-m-d 00:00:00',strtotime("$beginThisWeek -7 days"));
$endLastWeek=date('Y-m-d 23:59:59',strtotime("$beginLastWeek +6 days"));
echo "上周初:{$beginLastWeek}";echo "上周末:{$endLastWeek}";echo "<hr>";
2、获取本月&&上月起始时间戳和结束时间戳
//本月
$beginThismonth=mktime(0,0,0,date('m'),1,date('Y'));
$beginThismonth = date("Y-m-d H:i:s", $beginThismonth);
$endThismonth=mktime(23,59,59,date('m'),date('t'),date('Y'));
$endThismonth = date("Y-m-d H:i:s", $endThismonth);
echo "月初时间:{$beginThismonth}<hr />月末时间:{$endThismonth}<hr />";
//上月
// 法1:
$end = mktime(23,59,59,date("m") ,0,date("Y"));
// 法2:
$beginLastmonth=mktime(0,0,0,date('m')-1,1,date('Y'));
$m=date('Y-m-d', $beginLastmonth); //上个月的开始日期
$t=date('t',strtotime($m)); //上个月共多少天
$endLastmonth=mktime(23,59,59,date('m')-1,$t,date('Y'));
调用
$e1 = date('Y-m-d',$end);
$s=date('Y-m-d',$beginLastmonth);
$e=date('Y-m-d',$endLastmonth);
echo "法一 ———— 上月末:{$e1}<hr>法二 ———— 上月初:{$s}; 上月末:{$e}";