php1970
發布時間: 2025-04-23 07:30:36
『壹』 php 如何處理 1970前時間 時間戳失效了
php5.2+提供了DateTime類來處理這樣的問題,參考方案如下(請注意時區的處理):
//1、Unix時間戳轉日期functionunixtime_to_date($unixtime,$timezone='PRC'){$datetime=newDateTime("@$unixtime");//DateTime類的bug,加入@可以將Unix時間戳作為參數傳入$datetime->setTimezone(newDateTimeZone($timezone));return$datetime->format("Y-m-dH:i:s");}
//2、日期轉Unix時間戳
functiondate_to_unixtime($date,$timezone='PRC'){
$datetime=newDateTime($date,newDateTimeZone($timezone));
return$datetime->format('U');
}
echodate_to_unixtime("1900-1-3100:00:00");//輸出-2206425952
echo'<br>';
echounixtime_to_date(date_to_unixtime("1900-1-3100:00:00"));//輸出1900-01-3100:00:00
『貳』 php讀取mysql網頁顯示是1970-01-01 資料庫中是字元串類型,比如41036讀取出為日期為2012-05-07
一般時間都是用這樣方法存的,用date函數轉化就行了,<?php echo date('Y-m-d',資料庫數據);?>
熱點內容