1

I have tried to solve it by extracting the numeric part and then parsed it using date function. But it shows me some old date which I guess is not correct.

$datef = "1490914800000+0100";
$adada = date('Y-m-d H:i:s', $datef);
// Gives date 1987-10-13 18:31:28 which is an old date. Please suggest.
Indication
  • 95
  • 1
  • 9
  • Actually `1987-10-13 18:31:28` can be handled by MySQL. How do you plan to bring this data into MySQL? – Tim Biegeleisen Jan 26 '17 at 04:09
  • @TimBiegeleisen, my question is not related to the date 1987-10-13 18:31:28 not being inserted into database. This date is coming through an API and it is an expiry date of coupon or deal. The date can't be so old. Either it is formatted different than what I have done or something I haven't done... If you know something about the /Date(...)/ please focus. – Indication Jan 26 '17 at 04:15
  • You're using the API incorrectly. See [here](http://stackoverflow.com/questions/13477788/convert-epoch-time-to-date-php) for the correct way to do this. In addition, you will have handle the timezone shift. – Tim Biegeleisen Jan 26 '17 at 04:18

2 Answers2

5

One approach, well-covered by this SO question, is to use the DateTime() function to convert time in seconds since epoch to a date, and then display this date using format(). But there are two caveats with your data. First, you appear to have milliseconds since the epoch, which needs to be converted to seconds. Second, you also have a timezone shift, in hours, tagged to the end. I split your $datef string into two parts, epoch and timezone, then arrive at the number of seconds since epoch.

list($epoch, $timezone) = explode('+', $datef);

$epoch = ($epoch / 1000) + (substr($timezone, 0, 2)*60*60) +
         (substr($timezone, 2, 2)*60);
$dt = new DateTime("@$epoch");
echo $dt->format('Y-m-d H:i:s');

Output:

2017-03-31 00:00:00

Demo here:

PHP Sandbox

Community
  • 1
  • 1
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • `$timezone / 100` <- this expression means it is 100 seconds in an hour, and that would return wrong results for non-zero-minutes timezones. – zerkms Jan 26 '17 at 04:29
  • Plenty of those outside India actually https://www.timeanddate.com/time/time-zones-interesting.html – zerkms Jan 26 '17 at 04:30
  • Thank you very much Tim sir. But as I have read your suggested links as well. A solution is something like `$datef = "1490914800000+0100"; echo date("Y-m-d H:i:s", substr($datef, 0, 10));` gives slight different result. What is it? – Indication Jan 26 '17 at 04:31
  • 1
    @Indication it is you not taking timezone into account. – zerkms Jan 26 '17 at 04:32
  • I now understand all this. Thank you for your valuable time devoted to this question. – Indication Jan 26 '17 at 04:54
2

The time seems to be in milliseconds. You can add the timezone shift to the seconds. 1 hour = 3600 seconds.

$milliSeconds = intval("1490914800000");
$seconds = $milliSeconds/1000;
$date = date("Y-m-d H:i:s", $seconds);
Vishnu J
  • 501
  • 2
  • 10