Search code examples
phpdatetimeepochdate-conversion

In PHP why use @Epochtime


I came up this sample code at

http://www.epochconverter.com/programming/php

$epoch = 1483228800;
$dt = new DateTime("@$epoch");  // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m-d H:i:s'); // output = 2017-01-01 00:00:00

I want to understand why the @ symbol is used and why DateTime() does not convert if the input is as below

$dt  = new DateTime($epoch);

Solution

  • It is format recognized by parser. More about supported formats in official documentation

    Why @ sign? Well, let's take date 1970-01-01 00:33:37 as a timestamp - it is 2017. Looks like current year, doesn't it? So to make sure that you are passing timestamp, instead of something ambigous, it is prefixed with @.