Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to format atom date time

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() expects parameter 2 to be long, object given in /bla/bla.php

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

like image 837
Yaniv Golan Avatar asked Nov 04 '09 23:11

Yaniv Golan


3 Answers

strtotime is a wonderful function for converting date formats to Unix timestamps.

This will give you what you're after:

date('my format here', strtotime('2009-11-04T19:55:41Z'));
like image 72
Josh Leitzel Avatar answered Oct 07 '22 04:10

Josh Leitzel


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');
like image 41
Paul T. Rawkeen Avatar answered Oct 07 '22 04:10

Paul T. Rawkeen


Try using strptime:

$date = strptime($str, "Y-m-d\TH:i:s\Z");
like image 2
Dominic Rodger Avatar answered Oct 07 '22 03:10

Dominic Rodger