Home > Software design >  PHP setcookie expiry in UTC date format despite setting timezone in Carbon
PHP setcookie expiry in UTC date format despite setting timezone in Carbon

Time:10-30

I'm using Carbon in my Laravel 8 project to set an expiry in a generic function that I'm using throughout my project called createCookie.

I'd like my timezone to be in Europe/London format, but when setting the cookie and checking my browser tab I'm only seeing the value in UTC datetime format, what am I missing?

/**
 * Create a cookie
 */
public function createCookie($name, $value, $expires = 60)
{
    $expiry = Carbon::now()->addMinutes($expires)->setTimezone('Europe/London');
    setCookie($name, $value, $expiry->timestamp);
}

CodePudding user response:

According to the carbon interface, the @property int|float|string $timestamp represent "seconds since the Unix Epoch".

For seconds since the Unix Epoch there is no timezone, these are in Coordinated Universal Time / Temps Universel Coordonné (UTC), which strictly not a timezone but a time-standard.


Are you seeing the GMT timezone?


Now with an educated guess that setCookie borrows its semantics from PHP setcookie, lets compare this with the integer $expires parameter:

expires

The time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch. In other words, you'll most likely set this with the time() function plus the number of seconds before you want it to expire. Or you might use mktime(). time() 60*60*24*30 will set the cookie to expire in 30 days. If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes).

Note:

You may notice the expires parameter takes on a Unix timestamp, as opposed to the date format Wdy, DD-Mon-YYYY HH:MM:SS GMT, this is because PHP does this conversion internally.

Taken from the PHP manual verbatim. If you see especially the note, this might already explain what you see in the browser tab.

CodePudding user response:

I'm not sure but if you mean that you click the "lock icon" and chooses cookies "expires" gives you a different value than what u set in the code, that means that the expire date there is in the format of your browser? I can be wrong here :)

  • Related