识别日期格式

时间:2012-12-11 13:43:07

标签: php date datetime datetime-format

这是什么日期格式?

2012-06-08dT00:00:00Z

如何将php中的时间戳转换为此日期格式?

2 个答案:

答案 0 :(得分: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'

答案 1 :(得分: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';