Convert ISO 8601 to unixtimestamp

35,780

Solution 1

echo date("U",strtotime('2012-01-18T11:45:00+01:00'));

Solution 2

To convert from ISO 8601 to unixtimestamp :

strtotime('2012-01-18T11:45:00+01:00');
// Output : 1326883500

To convert from unixtimestamp to ISO 8601 (timezone server) :

date_format(date_timestamp_set(new DateTime(), 1326883500), 'c');
// Output : 2012-01-18T11:45:00+01:00

To convert from unixtimestamp to ISO 8601 (GMT) :

date_format(date_create('@'. 1326883500), 'c') . "\n";
// Output : 2012-01-18T10:45:00+00:00

To convert from unixtimestamp to ISO 8601 (custom timezone) :

date_format(date_timestamp_set(new DateTime(), 1326883500)->setTimezone(new DateTimeZone('America/New_York')), 'c');
// Output : 2012-01-18T05:45:00-05:00
Share:
35,780
Codler
Author by

Codler

https://www.linkedin.com/in/codler/

Updated on May 11, 2020

Comments

  • Codler
    Codler almost 4 years

    How can I convert 2012-01-18T11:45:00+01:00 (ISO 8601) to 1326883500 (unixtimestamp) in PHP?

  • 472084
    472084 over 12 years
    Thanks, didnt know strtotime knew about ISO dates
  • k102
    k102 over 12 years
    @Codler in this case no. i thought you may want to use another format of input/output
  • 夏期劇場
    夏期劇場 over 6 years
    Very useful! Thanks much!