Contact Us Forum Rules Search Archive
WebProWorld Part of WebProNews.com
Page One Link To Us Edit Profile Private Messages Archives FAQ RSS Feeds  
 

Go Back   WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum
Subscribe to the Newsletter FREE!


Register FAQ Members List Calendar Arcade Chatbox Mark Forums Read

Web Programming Discussion Forum Working with an API? Developing a plugin? Writing a Mod or script for your favorite blog, Web 2.0 site or Forum? Welcome.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-10-2006, 02:20 AM
WebProWorld Pro
 

Join Date: Jul 2003
Location: Canada
Posts: 268
ackerley1 RepRank 0
Default Date Specific If Statements with PHP

I am trying to set a promotional site up on my site so that it automatically displays the correct promotion depending on the date. For example from mid Jan to end of Feb a Valentines promo or Nov for a Thanksgiving promo and Dec for a christmas promo.

How would I write the if statements for this?

if (date >= ??? && date <= ???)
{
echo 'promo';
}
etc??

Any ideas?
__________________
Rob
Reply With Quote
  #2 (permalink)  
Old 04-10-2006, 08:00 AM
Easywebdev's Avatar
WebProWorld Veteran
 

Join Date: Apr 2004
Posts: 328
Easywebdev RepRank 1
Default

I'm not sure you can do a comparison on the date() function, never tried it.
The current time is stored as an integer so you can use that as the reference point and strtotime() to convert a date to a time integer.

Try this.

Code:
$now = time();
$promo_start = strtotime("15 January 2006");
$promo_end = strtotime("28 February 2006");

if (($now >= $promo_start) && ($now <= $promo_end))
{
promotion code
}
else
{
etc
}
Reply With Quote
  #3 (permalink)  
Old 04-10-2006, 05:51 PM
freehits's Avatar
WebProWorld Veteran
 

Join Date: Sep 2004
Location: Posse's On Broadway
Posts: 953
freehits RepRank 0
Default

I think you would make a timestamp for the start and end date, as well as the current date, and then compare?

mktime($hour, $minute, 0, $month, $day, $year, -1);

Just a variation on the classic countdown timer
http://elouai.com/countdown-clock.php
Reply With Quote
  #4 (permalink)  
Old 04-10-2006, 08:52 PM
WebProWorld Member
 

Join Date: Jan 2006
Location: USA
Posts: 41
krnl RepRank 0
Default

Well, this isn't the prettiest code, but it's quite simple and should work, I think...

Code:
<?
$month = date(m);
$day = date(d);

if (( $month == '12' ) && ( $day <= '25' ))
{
  doXmasPromo();
} 
else if ( $month == '11' )
{
  doThanksgivingPromo();
}
else if (( $month == '02' ) && ( $day < '20' ))
{
  doValentinePromo();
}
else
{
  doDefault();
}
?>
__________________
Rick - Linux Tutorials for Beginners
allin-enterprises.com | all-in-general.com | MagliteSales.com | DiscountFishTackle.com
Reply With Quote
  #5 (permalink)  
Old 04-10-2006, 10:17 PM
danlefree's Avatar
WebProWorld Member
 

Join Date: Jun 2005
Location: Seattle
Posts: 67
danlefree RepRank 0
Default

If you're in the market for a year-round solution, a switch() statement will probably serve you well.

Code:
<?php
  switch(date("m")) {
    case 1:
      echo "January promotion...";
    break;
    case 2:
      if(date("d")==14) {
        echo "Valentine's date promo...";
      } else {
        echo "February promotion...";
      }
    break;
// ... et cetera ...
    default:
      echo "No promo";
    break;
  }
?>
Reply With Quote
  #6 (permalink)  
Old 04-12-2006, 05:03 AM
WebProWorld Pro
 

Join Date: Jul 2003
Location: Canada
Posts: 268
ackerley1 RepRank 0
Default

Thanks krnl, That way worked very well. I will have to play with it a bit for date ranges (unless you have any ideas: Say from 15Jan to 28Feb)

If (($month == '1' || '2') && ($day == ????))

Or maybe just trigger the start of promo in the opening if, then immediately do another if to check for closing date?

What is used to break out of just the the current if statement? - break? die?

Thanks again everyone.
__________________
Rob
Reply With Quote
Reply

  WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum
Tags: , , ,



Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Search Engine Optimization by vBSEO 3.2.0