2012-12-11 66 views
-2

這是什麼日期格式?識別日期格式

2012-06-08dT00:00:00Z 

我該如何將php中的時間戳轉換爲此日期格式?

+0

爲什麼你需要這種特定的格式? –

+0

如果'd'不在那裏,它將是ISO 8601。 – dnagirl

+0

你確定這是ISO 8691嗎? 在PHP文檔它說:ISO-8601(例如:2005-08-15T15:52:01 + 0000) –

回答

0

這裏是東西給你,從開始:

$date = "2012-06-08dT01:02:03Z"; 

// parse the date correctly 
$parsed_date = date_parse_from_format('Y-m-d H:i:s ', $date); 
print_r($parsed_date); 

// Make time from parsed date 
$old_date = mktime($parsed_date['hour'], $parsed_date['minute'], $parsed_date['second'], $parsed_date['month'], $parsed_date['day'], $parsed_date['year']); 

$now_date = time(); 

// a silly way to print that parsed date into the original way as before 
echo date("Y-m-d", $old_date) . 'dT' . date("H:i:s", $old_date) . 'Z'; 
echo "\n"; 

// a silly way to print current date/time in that format 
echo date("Y-m-d", $now_date) . 'dT' . date("H:i:s", $now_date) . 'Z'; 
2
$dt = new DateTime('2012-06-08T00:00:00Z'); //with no 'd' 
$timestamp = $dt->format('U'); 

如果你必須有 'd',那麼:

$dt = DateTime::createFromFormat('Y-m-d??H:i:s?', '2012-06-08dT00:00:00Z'); 
$timestamp = $dt->format('U'); 

ETA:時間戳 - >您的格式

$dt = new DateTime('@1339124400'); //the @ indicates the following number is a timestamp 
$isoformat= $dt->format('Y-m-d\TH:i:sZ'); //leave out the 'd' and escape the 'T' 
+0

也許我不以正確的方式解釋我:我有unix時間戳,我想轉換它像'2012-06-08dT00:00:00Z'的格式(是的,也許d是一個錯誤) –

+0

@OscarFanelli:抱歉。我已經添加了時間戳格式的信息。 – dnagirl