php 如何格式化原子日期时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1677373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:28:39  来源:igfitidea点击:

how to format atom date time

phpparsingdatetimeatom-feed

提问by Yaniv Golan

I'm getting dates from feed in this format:

我以这种格式从提要中获取日期:

2009-11-04T19:55:41Z

I'm trying to format it using the date()function in PHP, but I get an error saying:

我正在尝试使用date()PHP 中的函数对其进行格式化,但收到一条错误消息:

date() expects parameter 2 to be long, object given in /bla/bla.php

date() 期望参数 2 很长,对象在 /bla/bla.php 中给出

I tried using preg_replace()to remove the Tand the Z, but still can't get it to work.

我尝试使用preg_replace()删除TZ,但仍然无法使其正常工作。

回答by Josh Leitzel

strtotimeis a wonderful function for converting date formats to Unix timestamps.

strtotime是将日期格式转换为 Unix 时间戳的绝妙函数。

This will give you what you're after:

这会给你你所追求的:

date('my format here', strtotime('2009-11-04T19:55:41Z'));

回答by Paul T. Rawkeen

What about

关于什么

\DateTime::createFromFormat(\DateTime::ATOM, $AtomDate); // converting Atom date to object

or

或者

date(\DateTime::ATOM, $timestamp); // formatting timestamp to Atom time

or both

或两者

$dto = \DateTime::createFromFormat(\DateTime::ATOM, $AtomDate);
date('d-M-Y H:i:s', $dto->getTimestamp()); // formatting Atom date to anything you want

or even better

甚至更好

$dto = \DateTime::createFromFormat(\DateTime::ATOM, $AtomDate);
$formattedDate = $dto->format('d-M-Y H:i:s');

or with time zone (as mentioned in comments)

或时区(如评论中所述

$dto = \DateTime::createFromFormat(
    \DateTime::ATOM,
    $ticketUpdatedAt,
    new \DateTimeZone('UTC')
);
$ticketUpdatedDate = $dto->format('Y-m-d H:i:s');

回答by Dominic Rodger

Try using strptime:

尝试使用strptime

$date = strptime($str, "Y-m-d\TH:i:s\Z");

回答by Brian Fisher

You can use the strtotimefunction.

您可以使用strtotime函数。

echo date('Y-M-D', strtotime($feedDate));

回答by Joey

That is the standard ISO 8601 combined date and time format given in UTC (hence the Z).

这是以 UTC 给出的标准 ISO 8601 组合日期和时间格式(因此是 Z)。

You might be able to parse it using

您也许可以使用

DateTime::createFromFormat('c', '2009-02-03');

or, if that fails (shouldn't, if PHP claims to understand ISO 8601), you can replace the Z by "+00:00".

或者,如果失败(不应该,如果 PHP 声称了解 ISO 8601),您可以将 Z 替换为“+00:00”。