set javascript cookies using server timestamp

Posted on 2004-11-23
Last Modified: 2008-02-01

   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.

Question by:sandeepaher
    LVL 27

    Accepted 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.
    LVL 48

    Expert Comment

    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);
    LVL 27

    Expert Comment

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing s…
    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now