如何将 YouTube 返回的日期解析为时间戳?

发布于 2024-10-07 09:09:27 字数 128 浏览 6 评论 0原文

YouTube 返回更新日期提交日期,如下所示:2010-08-22T04:46:18.000Z

是否有 PHP 函数,或者解析这个的日期掩码?

YouTube returns the Updated date and Submitted on date as follows: 2010-08-22T04:46:18.000Z

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑饮青盏花 2024-10-14 09:09:27
$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"
// }

这使用 DateTime 类。它能够识别时区和小数秒。要显示日期,请使用 格式方法:

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

要将日期转换为本地时区,请使用 setTimezone方法:

$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
$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
阳光①夏 2024-10-14 09:09:27

听起来 strtotime 就是您要寻找的。

编辑:如果这不起作用,请查看日期和时间类 - 有一些方法可以解析指定格式的日期(像这样 - 不直接返回时间戳,但是如果您从中构造一个 DateTime,则可以使用它的 getTimestamp-方法)

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)

背叛残局 2024-10-14 09:09:27

试试这个:

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

末尾的“z”似乎是 strtotime 的问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文