xRobot
xRobot

Reputation: 26567

Date is not stored correctly in a cookie

I stored this date in a cookie:

Tue Jun 26 2012 15:38:41 GMT+0200 (CEST)

and now I need to print it in this form:

06/26/2012 15:38

So I write this code:

echo $_COOKIE['date'];
echo date('m/d/Y g:i A', strtotime($_COOKIE['date'])); 

but I get this:

Tue Jun 26 2012 15:38:41 GMT 0200 (CEST)
12/31/1969 7:00 PM

instead of:

 06/26/2012 15:38

Why?

Upvotes: 1

Views: 243

Answers (3)

René Höhle
René Höhle

Reputation: 27295

Perhaps its easier to store the timestamp in the cookie and not the date string.

$_COOKIE['date'] = time();

and then read it direct to the date function.

i have tried this:

<?php
setcookie('date', time());
var_dump($_COOKIE);
echo date('m/d/Y g:i A', $_COOKIE['date']);
?>

and the result is: 06/25/2012 6:13 PM

Upvotes: 1

J. Bruni
J. Bruni

Reputation: 20492

Because strtotime is not recognizing the date format you are using.

Upvotes: 0

Mathieu
Mathieu

Reputation: 5695

strtotime does not seem to understand the date format, you can try to parse it with DateTime::createFromFormat or you can try to store it in another format

Upvotes: 2

Related Questions