PHP时间处理

https://www.cnblogs.com/iAmSoScArEd/

时间格式说明:

 

$time = time(); // 当前时间戳
var_dump($time);  // int(1516155874)

$time_str = date('Y-m-d H:i:s', $time); // 将时间戳转化为相应的时间字符串
var_dump($time_str);  // string(19) "2018-01-17 02:24:34"

$time_int = strtotime($time_str);  // 将时间字符串转化为时间戳
var_dump($time_int); // int(1516155874)

 

1、PHP基本常用的时间函数

date(): 把时间戳格式化为更易读的日期和时间

time(): 获取当前 Unix 时间戳

strtotime(): 将表示时间和日期的字符串转化为相应的时间戳

mktime(): 创建日期

2、date()函数

<?php
// 假定今天时间:March 10th, 2001, 5:16:18 pm
$today = date("Y-m-d H:i:s");                   // 2001-03-10 17:16:18 (MySQL datetime 格式)
$today = date("F j, Y, g:i a");                 // March 10, 2001, 5:16 pm
$today = date("m.d.y");                         // 03.10.01
$today = date("j, n, Y");                       // 10, 3, 2001
$today = date("Ymd");                           // 20010310
$today = date('h-i-s, j-m-y, it is w Day z ');  // 05-16-17, 10-03-01, 1631 1618 6 Fripm01
$today = date('\i\t \i\s \t\h\e jS \d\a\y.');   // It is the 10th day.
$today = date("D M j G:i:s T Y");               // Sat Mar 10 15:16:08 MST 2001
$today = date('H:m:s \m \i\s\ \m\o\n\t\h');     // 17:03:17 m is month
$today = date("H:i:s");                         // 17:16:17

date('Y'); // 当前年
date('m'); // 当前月
date('d'); // 当前日

3、strtotime()函数

<?php
//返回时间戳
echo strtotime("now"), "\n";         // 现在时间戳 echo strtotime("10 September 2000"), "\n"; // 2000年10月的现在时间的时间戳 echo strtotime("+1 day"), "\n"; // 距离现在一天后的时间戳 echo strtotime("-3 day"), "\n";       // 距离现在三天前的时间戳 echo strtotime("+1 week"), "\n";       // 距离现在一周后的时间戳 echo strtotime("-1 month"), "\n";      // 距离现在一个月前的时间戳 echo strtotime("+1 year"), "\n";       // 距离现在一年后的时间戳 echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n"; // 距离现在1周2天4小时2秒后的时间戳 echo strtotime("next Thursday"), "\n";   // 下个星期三 echo strtotime("last Monday"), "\n";   // 本月的最后一个星期一

4、mktime()函数

<?php
//任何给定月份的最后一天都可以被表示为下个月的第 "0" 天,而不是 -1 天
//参数位置分别代表 时,分,秒,月,天,年
$lastday = mktime(0, 0, 0, 3, 0, 2000); echo strftime("Last day in Feb 2000 is: %d", $lastday); $lastday = mktime(0, 0, 0, 4, -31, 2000); echo strftime("Last day in Feb 2000 is: %d", $lastday); ?>

 

posted @ 2019-05-19 01:11  我超怕的  阅读(536)  评论(0编辑  收藏  举报