Codarz360
Codarz360

Reputation: 436

Why am I getting an incorrect date from the timestamp I send through?

Here is my code and the date that should be returned is 2014-08-22 04:21:24 but for some reason I'm getting 1967-11-06 12:08:32 when passing in the timestamp of 1408681284000

    $test = convertDate();
    echo $test;
    function convertDate() {
        $test = date('Y-m-d H:i:s', 1408681284000);
        return $test;
    }

If anybody knows why this is happening then I would appreciate if you could help me out. Thanks.

Upvotes: 0

Views: 39

Answers (2)

Barmar
Barmar

Reputation: 780818

Your timestamp is in milliseconds, but PHP expects them to be seconds.

Divide the timestamp by 1000 and you'll get the time you expect:

$test = date('Y-m-d H:i:s', 1408681284);

I get 2014-08-22 00:21:24, but I'm in a different timezone from you.

Upvotes: 2

Jay
Jay

Reputation: 2686

Your time is in milliseconds you should change it to seconds.

If you don't want to use seconds you can use:

strtotime() with date() into whatever format you want.

$mydate = "2014-08-22";
$newDate = date("d-m-Y", strtotime($myDate));

Upvotes: 0

Related Questions