PHP date() time() 应用总结 - 日期时间计算,格式化,转化,间隔天数

工作中经常碰到日期处理的事情,比如格式化日期、某日期的时间戳、几天后的时间戳、某日期几天前的日期、某日期几天后的日期和日期间相隔天数等。常用到的日期处理函数有date(),strftime(),strtotime(),time(),mktime(),strptime(),localtime(),getdate()。下面就这些函数做简单介绍,并列举一些常用事例。主要理解函数的参数及返回值,再结合事例加深印象。

一:把时间戳转化成格式化日期

string date ( string $format [, int $timestamp ] ),把时间戳转化成格式化日期,如果没有给出时间戳,默认是当前日期时间。
string strftime ( string $format [, int $timestamp = time() ] ),把时间戳转化成格式化日期,如果没有给出时间戳,默认是当前日期时间。

二:返回日期时间戳

int strtotime ( string $time [, int $now = time() ] ) ,将任何字符串的日期时间描述解析为Unix时间戳。
int time ( void ) ,返回当前的 Unix 时间戳。
int mktime(),返回一个日期的Unix时间戳。

三:返回日期数组

array strptime ( string $date , string $format ),返回任何日期的日期数组,WIN系统下没有此函数。$date:任何日期,$format:跟$date相同的日期格式。包括tm_sec、tm_min、tm_hour、tm_mday、tm_mon、tm_year、tm_wday、tm_yday


array localtime ([ int $timestamp = time() [, bool $is_associative = false ]] ),返回任何Unix时间戳的日期数组。包括tm_sec、tm_min、tm_hour、tm_mday、tm_mon、tm_year、tm_wday、tm_yday


array getdate ([ int $timestamp = time() ] ),返回日期时间数组。$timestamp是Unix时间戳。默认是time()的值。包括seconds、minutes、hours、mday、wday、mon、year、yday、weekday、month。

四:微秒数

mixed microtime ([ bool $get_as_float ] ) ,返回当前 Unix 时间戳和微秒数。如果给出了 get_as_float 参数并且其值等价于 TRUE,microtime() 将返回一个浮点数。

下面介绍几个常用例子,加深理解。

<?php

echo str_repeat('*',20),"<br>";
echo '当前日期时间 date(\'Y-m-d H:i:s\') : ',date('Y-m-d H:i:s'),"<br>";
echo '时间戳转格式化日期:strftime(\'%Y-%m-%d %H:%M:%S\',1536566075) : ',strftime('%Y-%m-%d %H:%M:%S',1536566075),"<br>";

echo '指定日期的时间戳 strtotime(\'2018-09-01 15:54:35\') :',strtotime('2018-09-01 15:54:35'),"<br>";
echo 'strtotime(\'2018-09-10 15:54:35\') :',strtotime('2018-09-10 15:54:35'),"<br>";
echo 'strtotime(\'+10 second\',1536566075) :',strtotime('+10 second',1536566075),"<br>";
echo 'strtotime(\'+1 hour\',1536566075) :',strtotime('+1 hour',1536566075),"<br>";
echo '当前时间戳 time() :',time(),"<br>";
echo '一天后的时间戳 time() + (24*3600) :',time()+(24*3600),"<br>";
echo '某日期时间后的某天:date(\'Y-m-d H:i:s\',strtotime(\'+2 day\',strtotime(\'2018-09-01 15:54:35\'))) :',date('Y-m-d H:i:s',strtotime('+2 day',strtotime('2018-09-01 15:54:35'))),"<br>";
echo '日期间相隔天数:round((strtotime(\'2018-09-01 15:54:35\')-strtotime(\'2018-08-30 10:04:15\'))/(3600*24)) :',round((strtotime('2018-09-01 15:54:35')-strtotime('2018-08-30 10:04:15'))/(3600*24)),"<br>";
echo 'var_dump(localtime(1536566075,true))',"<br>";
var_dump(localtime(1536566075,true));
echo "<br>";
echo 'var_dump(getdate(1536566075))',"<br>";
var_dump(getdate(1536566075));
echo "<br>";

echo "<br>";
echo '上月第一天 :date(\'Y-m-d\',strtotime(\'first day of last month\')) :'.date('Y-m-d',strtotime('first day of last month'))."<br>";
echo '上月最后一天:date(\'Y-m-d\',strtotime(\'last day of last month\')) :'.date('Y-m-d',strtotime('last day of last month'))."<br>";
echo '某天的上月第一天 :date(\'Y-m-d\',strtotime(\'first day of last month\',strtotime(\'2018-05-06\'))) :'.date('Y-m-d',strtotime('first day of last month',strtotime('2018-05-06')))."<br>";
echo '某天的上月最后一天 :date(\'Y-m-d\',strtotime(\'last day of last month\',strtotime(\'2018-05-06\'))) :'.date('Y-m-d',strtotime('last day of last month',strtotime('2018-05-06')))."<br>";

猜你喜欢

转载自blog.csdn.net/uvyoaa/article/details/82619873
今日推荐