0

I'm creating a cookie in JavaScript with this code. I actually changed the code a bit:

function setCookie (name,value,days) {
  var expires, newValue;
    if (days) {
      var date = new Date(); // days = 0.0006944444; // testing with one minute
      date.setTime(date.getTime()+(days*24*60*60*1000));
      expires = "; expires="+date.toString(); 
      newValue = encodeURIComponent(value)+'|'+date+expires;
  } else expires = "";
  document.cookie = name+"="+(newValue)+"; path=/";
}

So the above function sends encodeURIComponent(value)+'|'+date+expires as value. In PHP I can do explode('|',$_COOKIE['my-key']) with the date formatted like this:

$string_time = "Fri Oct 06 2017 19:34:44 GMT 0300 (Eastern European Summer Time);

Now I need to convert this string to integer to be compared against the PHP's time() integer format.

Doing the following:

$currentTime = date('YmdHis', time());
$expire_time = date('YmdHis', strtotime($string_time));

It actually outputs this:

string(14) "19700101000000" // $currentTime
string(14) "20171006162139" // $cookie_time

Question why is $currentTime always the same 19700101000000 value?

thednp
  • 4,401
  • 4
  • 33
  • 45
  • This is confusing? Clearly you aren't getting the expire time from the set cookie, but somehow from the script creating an UTC date. Why don't you just get that as a unix timestamp instead. – adeneo Oct 06 '17 at 16:33
  • This is the first time I'm doing this, perhaps you can shed some light on which value should be set as UNIX timestamp? – thednp Oct 06 '17 at 16:35
  • Where are you getting `$string_time` from, and how did you get it to the server? – adeneo Oct 06 '17 at 16:36
  • I will update the question in a sec. Please hold on. – thednp Oct 06 '17 at 16:38
  • `$string_time` is not a valid [date format](http://php.net/manual/en/datetime.formats.php), where your resulting variable declarations seem to be reversed. https://3v4l.org/QT40b Note that [`date_create()`](http://php.net/manual/en/datetime.construct.php) returns false on error. You would need to use [`DateTime::createFromFormat()`](http://php.net/manual/en/datetime.createfromformat.php); Another note that the 32bit unix timestamp can only go to year 2038 – Will B. Oct 06 '17 at 16:43
  • @adeneo I updated the question please have a look. – thednp Oct 06 '17 at 16:47
  • 1
    `Fri Oct 06 2017 19:34:44 GMT 0300 (Eastern European Summer Time)` is not a valid date format for use in PHP. https://3v4l.org/KdFuP – Will B. Oct 06 '17 at 16:49
  • Thanks. I will change the PHP and JavaScript code to always use the PHP timestamp anyway, I have an idea. – thednp Oct 06 '17 at 16:56
  • 1
    You could also use `DateTime::createFromFormat('D M d Y H:i:s T O \(\E\a\s\t\e\r\n \E\u\r\o\p\e\a\n \S\u\m\m\e\r \T\i\m\e\)', $string_time);` but would need to add a `+` to the timezone offset. https://3v4l.org/ZQi4g – Will B. Oct 06 '17 at 17:19
  • I was about to do just that, but adeneo gave me the right answer. Thank you. – thednp Oct 06 '17 at 18:33

1 Answers1

2

Just use a unix timestamp instead, as you're not getting the time from the expries settings, but from the cookies value

function setCookie (name,value,days) {
  var expires, newValue;

  if (days) {
      var date = new Date();
      date.setTime(date.getTime()+(days*24*60*60*1000));
      expires = "; expires="+date.toUTCString(); 
      newValue = date.getTime() / 1000;
  } else {
      expires = "";
  }
  document.cookie = name+"="+(newValue)+"; path=/";
}

Now you can compare it directly to the PHP unix timestamp from time() and get the difference in seconds.

Note that you're not even using the expires variable, so this does nothing when it comes to how long the cookie is valid.

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • Not all days are 24 hours long where daylight saving is observed, so `(days*24*60*60*1000)` may not be the correct number of milliseconds: [*How to add number of days to today's date?*](https://stackoverflow.com/questions/3818193/how-to-add-number-of-days-to-todays-date). – RobG Oct 06 '17 at 23:13
  • @RobG - a unix timestamp is the number of seconds, or in javascript the number of milliseconds, since epoch, it doesn't contain any timezone data or daylight savings, so there's nothing to account for. – adeneo Oct 07 '17 at 07:55
  • The function has a parameter *days* but sets the value using milliseconds. In local time, a cookie set at 4am on one day may expire at 3am or 5am (assuming a daylight saving offset of 1 hour) on some following day, i.e. a multiple of 24 elapsed hours but slightly more or less in days. – RobG Oct 07 '17 at 23:34
  • @RobG - there is no local time, nor daylight savings, it all uses UTC time since epoch ? – adeneo Oct 08 '17 at 05:22