json date convert to timestamp in php


You can use regular expression to fetch your timestamp and offset:

preg_match('~(\d+)\d{3}((?:\+|-)\d+)~', $sale['Date'], $match);
print_r($match);

return will be:

Array
(
    [0] => 1386834199000+0200
    [1] => 1386834199
    [2] => +0200
)

With fetched timestamp you can now format your datetime. When formating timestamp, you can also take offset into account (given one, or set your timezone), if needed so:

$dt = new DateTime('@' . $match[1]);
$dt = new DateTime($dt->format('Y-m-d H:i:s') . ' ' . $match[2]);
# $dt->setTimezone(new DateTimezone("your_timezone"));
print_r($dt);

return will be DateTime object:

DateTime Object
(
    [date] => 2013-12-12 07:43:19
    [timezone_type] => 1
    [timezone] => +02:00
)

which can be formated for your needs:

$createdDate = dt->format('d-m-Y H:i:s');
Advertisements

One thought on “json date convert to timestamp in php

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s