在php中可以通過strtotime函數把時間轉為時間戳,其語法是“strtotime(time,now);”,參數time表示日期時間字符串,而參數now是用來計算返回值的時間戳。
推薦:《PHP視頻教程》
strtotime() 函數將任何英文文本的日期或時間描述解析為 Unix 時間戳(自 January 1 1970 00:00:00 GMT 起的秒數)。
注意:如果年份表示使用兩位數格式,則值 0-69 會映射為 2000-2069,值 70-100 會映射為 1970-2000。
注意:請注意 m/d/y 或 d-m-y 格式的日期,如果分隔符是斜線(/),則使用美洲的 m/d/y 格式。如果分隔符是橫杠(-)或者點(.),則使用歐洲的 d-m-y 格式。為了避免潛在的錯誤,您應該盡可能使用 YYYY-MM-DD 格式或者使用 date_create_from_format() 函數。
語法
strtotime(time,now);
參數
time 必需。規定日期/時間字符串。
now 可選。規定用來計算返回值的時間戳。如果省略該參數,則使用當前時間。
實例:
<?php echo(strtotime("now") . "<br>"); echo(strtotime("5 September 2016") . "<br>"); echo(strtotime("+5 hours") . "<br>"); echo(strtotime("+1 week") . "<br>"); echo(strtotime("+1 week 3 days 7 hours 5 seconds") . "<br>"); echo(strtotime("next Monday") . "<br>"); echo(strtotime("last Sunday")); ?>
運行結果是:
1604901378 1473004800 1604919378 1605506178 1605790583 1605456000 1604764800