1

How to get the cookie's birthday/time in PHP?

The cookie itself contains other data, so was wondering if there is a way to get the exact date and h:m:s when it was created without having to create a second cookie just to hold that information?

I searched but too much interference with the other keywords: set, time, cookie, date that's showing answers, but not to this question.

NickNo
  • 872
  • 15
  • 32

1 Answers1

1

What you can do is to create an array, serialize that array to a string and store the serialized string inside the cookie:

$mycookieArray = array('cookiedata'=>'some data','creationTime'=>new DateTime());
$mycookiedata = serialize($mycookieArray);

Then, everytime you want to load the correct you have to unserialize the array from the cookiedata:

$mycookieArray = unserialize($mycookiedata);
Alireza Fallah
  • 4,609
  • 3
  • 31
  • 57
J. Rahmati
  • 735
  • 10
  • 37
  • You are assuming I am allowed to change the original cookie, but alas, with that power comes great responsibility I was told that takes years to earn. :( – NickNo Jan 30 '14 at 22:29
  • Thanks, I will give it a day or two, if no one thinks of anything better I will check you off. – NickNo Jan 31 '14 at 00:22
  • 1
    Sorry, even what I commented earlier wasn't a correct reference. (i.e. metadata for a specific cookie vs global cookie settings)! According to [this](http://stackoverflow.com/questions/2839732/get-cookie-expiration) SO link, only name and value are stored! – J. Rahmati Jan 31 '14 at 18:00