开发者

How to parse YouTube returned date to timestamp?

开发者 https://www.devze.com 2023-01-30 10:56 出处:网络
YouTube returns the Updated date and Submitted on date as follows: 2010-08-22T04:4开发者_StackOverflow社区6:18.000Z

YouTube returns the Updated date and Submitted on date as follows: 2010-08-22T04:4开发者_StackOverflow社区6:18.000Z

Is there a PHP function, or a date mask that parses this?


$dt = DateTime::createFromFormat("Y-m-d\TH:i:s.uP", "2010-08-22T04:46:18.000Z");
var_dump($dt);
// object(DateTime)#1 (3) {
//   ["date"]=>
//   string(26) "2010-08-22 04:46:18.000000"
//   ["timezone_type"]=>
//   int(2)
//   ["timezone"]=>
//   string(1) "Z"
// }

This uses the DateTime class. It is timezone and fractional seconds aware. To display the date use the format method:

echo $dt->format("Y-m-d H:i:s e");
// 2010-08-22 04:46:18 Z

To convert the date to local timezone use the setTimezone method:

$dt->setTimezone(new DateTimeZone(date_default_timezone_get()));
echo $dt->format("Y-m-d H:i:s e");
// 2010-08-21 21:46:18 America/Los_Angeles


sounds like strtotime is what you're looking for.

EDIT: if this doesn't work, take a look at the Date and Time classes - there are methods for parsing dates in specified formats (like this - doesn't return a timestamp directly, but if you construct a DateTime from this, you can use it's getTimestamp-method)


Try this:

$date=substr("2010-08-22T04:46:18.000z",0,strlen("2010-08-22T04:46:18.000z")-1);
$stamp=strtotime($date);

The "z" at the end seems to be the problem for strtotime.

0

精彩评论

暂无评论...
验证码 换一张
取 消