ProDraz
ProDraz

Reputation: 1281

Date / Time showing odd value in PHP

I have wierd issues with time / date in PHP this year. Code have not changed at all and my dates are bugged.

Code is for example:

$date = strtotime($order['date']);

$dateNew = date('Y-m-d h:i A', $date);

print $dateNew;

Returns 1969-12-31 07:00 PM for some reasson, altough:

print $order['date'];

Returns 2013-01-12 18:25:43

I'm confused because I'm quite sure that my code is correct.

I dare you to solve this bugger!

Upvotes: 0

Views: 250

Answers (3)

Xavier
Xavier

Reputation: 4007

The function strtotime() was made for transform English into date format.

The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.

As i don't know what is really into your $order variable i will suggest 2 solutions :

Maybe you can avoid the strtotime function and replace it by date() directly like this :

$order = ['date' => '2013-01-12 18:25:43'];
$date = date($order['date']);

It works well here: http://codepad.viper-7.com/cbNA87

Or, if it's not working consider to use mktime(), it will convert the date into seconds since the epoch.

The Unix epoch is the reference point for all time stamps. PHP calculates the times from this date in seconds. The $date should be null and your server in the east coast of the US so it's returns the epoch :)

Upvotes: 1

Mike Graf
Mike Graf

Reputation: 5307

Unexpected dates of "1969-12-31 07:00 PM" means something went wrong with date() .

your strototime($order['date']) is probably returning false (failing to parse it to a unix timestamp).

Try this and ensure its returning an int (not false)

 var_dump($order['date'], strtotime($order['date'])); 

See the error state of date: http://php.net/date See the return values of strtotime: http://php.net/strtotime

Upvotes: 1

Kyle
Kyle

Reputation: 1733

PHP returns the date 1969-12-31 when there is not a proper date. So if you did

$date = 0;
$dateNew = date('Y-m-d', strtotime($date));

Your result would be 1969-12-31, since that is the default Unix epoch time. http://php.net/manual/en/function.time.php

Upvotes: 1

Related Questions