2017-04-19 111 views
0

我想將時間字符串轉換爲datetime與子午線,但我得到錯誤的日期時間。
這裏是我的代碼:php日期時間和區域

$timeZone = 'Europe/Warsaw'; 
date_default_timezone_set($timeZone); 
$millis = 1540399680000; 
$seconds = $millis/1000; 
$date = date("d/m/Y H:i:s a", $seconds); 
debug($date); 
$timeZone = new DateTimeZone('Europe/Warsaw'); 
$formato = "d/m/Y H:i:s a"; 
$fecha = DateTime::createFromFormat($formato, $date, $timeZone); 
debug($fecha); 
debug($fecha->format('Y')); 
debug($fecha->format('a')); 

結果進行調試($日期)

24/10/2018 18:48:00 pm 

結果進行調試($出生日期)

object(DateTime) { 
    date => '2018-10-25 06:48:00.000000' 
    timezone_type => (int) 3 
    timezone => 'Europe/Warsaw' 
} 

什麼,我期待在調試( $出生日期); date => '2018-10-24 06:48:00.000000'

+1

你期望得到什麼? – alistaircol

+0

'$ fecha'這是一個日期時間對象,您可以根據需要進行格式化。 – tarikul05

+0

'debug($ fecha-> ​​format('d/m/Y H:i:s a'));'它會給你想要的解決方案 – tarikul05

回答

-1

基於文檔(http://php.net/manual/en/datetime.createfromformat.php),DateTime createFormFormat函數返回一個DateTime對象。如果您想獲得直接輸出,請按照此代碼進行操作。

$timeZone = 'Europe/Warsaw'; 
date_default_timezone_set($timeZone); 
$millis = 1540399680000; 
$seconds = $millis/1000; 
$date = date("d/m/Y H:i:s a", $seconds); 
debug($date); 
$timeZone = new DateTimeZone('Europe/Warsaw'); 
$formato = "d/m/Y H:i:s a"; 
// instead of this 
//$fecha = DateTime::createFromFormat($formato, $date, $timeZone); 
// add ->format in the end 
$fecha = DateTime::createFromFormat($formato, $date, $timeZone)->format($formato); 
debug($fecha); 
0

您是否嘗試過使用CakeTime::format()? F.ex。:

$date = CakeTime::format($seconds, 'd/m/Y H:i:s a', false, 'Europe/Warsaw'); 
debug($date);