3

Ebay API provides me time in ISO 8601.

For example, it gives me item time left: P0DT0H1M4S Here is how this time is formatted: ebay duration

I want to convert this time to simple Y-m-d H:i:s format. So far I've tried date("Y-m-d", strtotime("P0DT0H1M4S")); but it does not work.

Cray
  • 2,774
  • 7
  • 22
  • 32
casper123
  • 1,736
  • 4
  • 21
  • 39
  • possible duplicate of [Using PHP: How convert an ISO8601 date to a different format](http://stackoverflow.com/questions/6458585/using-php-how-convert-an-iso8601-date-to-a-different-format) – Mike B Apr 17 '14 at 19:34
  • 1
    Maybe not, the date format in that question doesn't use the duration format like in your example. – Mike B Apr 17 '14 at 19:36
  • @MikeB: I've already seen this question, no luck :( – casper123 Apr 17 '14 at 19:48

1 Answers1

3

That date interval string is an ISO 8601 duration specification and can be use with DateTime() and DateInterval()

$date = new DateTime();
$date->add(new DateInterval('P0DT0H1M4S'));
echo $date->format('Y-m-d H:i:s');

or as a one-liner (PHP 5.4+)

echo (new DateTime())->add(new DateInterval('P0DT0H1M4S'))->format('Y-m-d H:i:s');

Demo

John Conde
  • 217,595
  • 99
  • 455
  • 496