Results 1 to 7 of 7

Thread: Useful time-based content code snippets

  1. #1
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    1,963
    Thanks
    16
    Thanked 305 Times in 304 Posts
    Blog Entries
    11

    Default Useful time-based content code snippets

    Hi,

    I've been using some php code to display time-based, promotional content on my website, which is set to expire on a certain date. Its really useful in the sense that you dont have to sit by your computer to make website updates on the dates specified - you can code your pages well in advance and the scripts automatically switches content over on the defined days.

    Anyway, I thought the code might be useful to others so here it is.

    Please note, I found parts of the code snippets on other forums/the web and have slightly modified them and mashed them together for my own needs. They're not rocket science but I don't want to claim to have written them myself.


    Code to hide content after a certain date:
    Code:
    <?php if(mktime() < mktime(0,0,0,07,20,2009)) { ?>
    Put your current content here that you want to hide after expiry date above.
    (Note: 07,20,2009 = MONTH, DAY, YEAR) 
    <?php } ?>
    Code to show alternative content after a certain date:
    Code:
    <?php 
    $exp_date = "2009-07-27";
    $todays_date = date("Y-m-d");
    $today = strtotime($todays_date);
    $expiration_date = strtotime($exp_date);
    if ($expiration_date > $today) 
    { ?>
    <!-- put current content here -->
    CURRENT (any HTML)
    <?php } else { ?>
    <!-- put expired content here -->
    EXPIRED (any HTML)
    <?php } ?>
    Code to show content1 upto a certain date, content2 for a limited time (between 2 defined dates), and content3 after the expiry date
    (ideal for promotional offers):

    Code:
    <?php 
    $exp_date = "2009-07-20";
    $exp_date2 = "2009-07-27";
    $todays_date = date("Y-m-d");
    $today = strtotime($todays_date);
    $expiration_date = strtotime($exp_date);
    $expiration_date2 = strtotime($exp_date2);
    if ($expiration_date > $today)
    { ?>
    <!-- put pre-promotion week content here -->
    e.g. - DON'T MISS OUR DISCOUT WEEK - COMING SOON (any HTML)
    <?php } else if (($expiration_date2 > $today) & ($expiration_date < $today)) { ?>
    <!-- put promotion week content here -->
    e.g. - IT'S DISCOUNT WEEK - GRAB YOUR BARGAINS (any HTML)
    <?php } else { ?>
    <!-- put expired/post-promotion week content here -->
    e.g. - PRICES ARE BACK TO NORMAL (any HTML)
    <?php } ?>
    I hope somebody finds it useful.

  2. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    1,963
    Thanks
    16
    Thanked 305 Times in 304 Posts
    Blog Entries
    11

    Default

    Correction to the following code snippet:

    Code to show content1 upto a certain date, content2 for a limited time (between 2 defined dates), and content3 after the expiry date
    (ideal for promotional offers):


    Code:
    Code:
    <?php 
    $exp_date = "2009-07-20";
    $exp_date2 = "2009-07-27";
    $todays_date = date("Y-m-d");
    $today = strtotime($todays_date);
    $expiration_date = strtotime($exp_date);
    $expiration_date2 = strtotime($exp_date2);
    if ($expiration_date > $today)
    { ?>
    <!-- put pre-promotion week content here -->
    e.g. - DON'T MISS OUR DISCOUT WEEK - COMING SOON (any HTML)
    <?php } else if ($expiration_date2 > $today) { ?>
    <!-- put promotion week content here -->
    e.g. - IT'S DISCOUNT WEEK - GRAB YOUR BARGAINS (any HTML)
    <?php } else { ?>
    <!-- put expired/post-promotion week content here -->
    e.g. - PRICES ARE BACK TO NORMAL (any HTML)
    <?php } ?>

  3. #3
    Join Date
    Mar 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Can I do it in HTML and by Following GMT/UTC Time??
    Please answer me, this is really important.
    and thanks to share here.

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    HTML doesn't allow anything like this-- you can't check the time, and you can't use "if" to sometimes show one thing and other times show something else. You need a serverside language like PHP (or ASP.net, CGI, SSI, etc.), or a clientside language (probably Javascript), not just HTML.

    You can use GMT because you just need to base your times on that. Your server will be configured for a certain timezone, so you can either change the timezone setting, or you can simply set this for another time (for example, add "+6" to all of your hours, or whatever is required.)
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Dec 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Oh my word! this could be exactly what I'm after! How would one add time of day to this equation?
    <?php
    $exp_date = "2009-07-27";
    $todays_date = date("Y-m-d");
    $today = strtotime($todays_date);
    $expiration_date = strtotime($exp_date);
    if ($expiration_date > $today)
    { ?>
    <!-- put current content here -->
    CURRENT (any HTML)
    <?php } else { ?>
    <!-- put expired content here -->
    EXPIRED (any HTML)
    <?php } ?>
    Are the three zeros the hours/mins/secs in this one?
    <?php if(mktime() < mktime(0,0,0,07,20,2009)) { ?>

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. #7
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by deliwasista View Post
    Oh my word! this could be exactly what I'm after! How would one add time of day to this equation?
    Some streamlining...

    PHP Code:
    <?php
    # the current timestamp is available in $_SERVER['REQUEST_TIME'] since version 5.1,
    # so, there's no need to use time()
    $now $_SERVER['REQUEST_TIME'];

    # add the desired expiration date/time
    # I prefer the HTTP Cookie date/time format (sans day name), 
    # but you can use anything from http://www.php.net/manual/en/datetime.formats.php
    $expiry strtotime'30 Mar 2013 12:00:00 UTC' );

    if( 
    $now $expiry ){

        
    /* before noon, March 30, 2013 UTC */

    }else{

        
    /*  after noon, March 30, 2013 UTC */

    }
    Keep in mind that time() and date() (et.al.) use unix timestamps, which handle a limited range of dates (typically, Fri, 13 Dec 1901 20:45:54 UTC (or, January 1 1970 00:00:00 GMT on some systems) until Tue, 19 Jan 2038 03:14:07 UTC; more on 64-bit systems).

    For more flexibility (both in range and functionality), you should look at the DateTime class.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •