Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 703
  • Last Modified:

set javascript cookies using server timestamp


   I want to assign server's time calculated using php  as expiration date for javascript cookie.
  I have timestamp in php which i want to use to set expiration date of javacsript cookies...but javascript cookie is not taking that timestamp...I would like to set cookie for 1 day ...but requirement is if user login at Tue  Nov 22 17.45.33  UTC so my cookie's expiration date should be Wed Nov 23 00.00.00 UTC ..means exact 1 day but not 24 hrs..It shoud set to 00.00.00 clock time  of next day.

  • 2
1 Solution
Get your timestamp in php using strtotime("+1 Day");


$var = strtotime("+1 Day");
echo date("m d Y H:i:s", $var);

Outputs: 11 25 2004 00:00:00

You should then just be able to write that timestamp in your JS code, eg:

<script langauge="JavaScript">
var stamp = <?php echo strtotime("+1 Day"); ?>


Although i have to wonder why you are using JS to set the cookie, i seem to recall the client side code to set a cookie is quite lengthy comparred to the signle line with PHP.
You can set cookies via php like to the desired expire-time like this:

$expire = mktime(0,0,0, date('m'), date('d') +1, date('Y'));
foreach ($_COOKIE as $n => $v) {
    setcookie($n, $v, $expire);
If you need it in the specific format of Wed Nov 23 00.00.00 UTC rather then just a timestamp you could use:
<?php echo date("D M d H.i.s \U\TC",strtotime("+1 Day")); ?> which outputs: Wed Nov 24 00.00.00 UTC

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now