php獲取時(shí)間戳的方法:可以利用time()函數(shù)或strtotime()函數(shù)來獲取。time()函數(shù)返回一個(gè)包含當(dāng)前時(shí)間的Unix時(shí)間戳的整數(shù)。strtotime()函數(shù)將任何字符串的日期時(shí)間描述解析為Unix時(shí)間戳。
time() 函數(shù)返回自 Unix 紀(jì)元(January 1 1970 00:00:00 GMT)起的當(dāng)前時(shí)間的秒數(shù)。返回一個(gè)包含當(dāng)前時(shí)間的 Unix 時(shí)間戳的整數(shù)。
(推薦教程:php圖文教程)
代碼實(shí)現(xiàn):
<?php $t=time(); echo($t . "<br>"); echo(date("Y-m-d",$t)); ?>
strtotime() 函數(shù)將任何字符串的日期時(shí)間描述解析為 Unix 時(shí)間戳(自 January 1 1970 00:00:00 GMT 起的秒數(shù))。
語法:
int strtotime ( string $time [, int $now = time() ] )
(視頻教程推薦:php視頻教程)
參數(shù):
-
time 必需。規(guī)定日期/時(shí)間字符串。
-
now 可選。規(guī)定用來計(jì)算返回值的時(shí)間戳。如果省略該參數(shù),則使用當(dāng)前時(shí)間。
代碼實(shí)現(xiàn):
<?php // 設(shè)置時(shí)區(qū) date_default_timezone_set("PRC"); $time = strtotime("2018-01-18 08:08:08"); // 將指定日期轉(zhuǎn)成時(shí)間戳 // 打印當(dāng)前時(shí)間 PHP_EOL 換行符,兼容不同系統(tǒng) echo $time, PHP_EOL; //