Log in

View Full Version : Useful time-based content code snippets



Beverleyh
07-21-2009, 10:54 AM
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:

<?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:

<?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):

<?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.

Beverleyh
08-17-2009, 07:46 AM
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:

<?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 } ?>

allursolve
03-23-2013, 02:12 AM
Can I do it in HTML and by Following GMT/UTC Time??
Please answer me, this is really important.
and thanks to share here.

djr33
03-23-2013, 05:00 AM
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.)

deliwasista
03-24-2013, 11:45 PM
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)) { ?>

djr33
03-25-2013, 12:09 AM
http://php.net/manual/en/function.mktime.php
http://www.php.net/manual/en/function.time.php
http://www.php.net/manual/en/function.date.php

All of the information you need should be available on those pages.

traq
03-25-2013, 01:30 AM
Oh my word! this could be exactly what I'm after! How would one add time of day to this equation?

Some streamlining...


<?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 (http://php.net/datetime) class.