PHP Time Countdown

July 22nd | | Uncategorized | Tags: None

I recently tried to add a feature to one of my client’s sites, which was a countdown till the next event, assume each event runs on a particular day at a particular time?
Normally, it runs fine with JavaScript, but then again, you don’t want people stealing your codes?

Anyways, the 1st time i wrote it, i just used basic PHP with LITTLE testing

if (date('D')=='Thu'&&date('G')<13) {$Quack = strtotime('Next 1pm');}
elseif (date('D')=='Thu'&&date('G')==13) {$Quack = strtotime('now');}
elseif (date('D')=='Thu'&&date('G')>13) {$Quack = strtotime('Next Thursday +13 hours'); $Quack=$Quack+604800;}
else {$Quack=strtotime('Next Thursday +13 hours');}
echo date('r',$Quack).'<br />';
$quackarray = getdate($Quack);
echo '<img src="http://www.ngpriest.com/Time.php?month='.$quackarray['mon'] .'&day='.$quackarray['mday'].'&hour='.$quackarray['hours'].'&min=0" /><br />';
  

After you test it, assuming you test it on Thursday before 1pm, you’ll get a weird number, since PHP doesn’t understand “Next 1pm”

And for the 2nd elseif, i couldn’t get it to calculate 1pm, so i told it to count to Thursday and add 13 hours
Unfortunately, if today is Thursday, it uses today’s date, then adds 13 hours, so the +604800 is used to add 1 week to the Thursday, therefore, pushing it up 1 week, since it’s Thursday, but already over!
Anyways, i rewrote the code here 😀

if (date('D')=='Thu'&&date('G')<13) {$Quack = strtotime('1300');}
elseif (date('D')=='Thu'&&date('G')==13) {$Quack = strtotime('now');}
elseif (date('D')=='Thu'&&date('G')>13) {$Quack = strtotime('Next Thursday +13 hours'); $Quack=$Quack+604800;}
else {$Quack=strtotime('Next Thursday +13 hours');}
echo date('r',$Quack).'<br />';
$quackarray = getdate($Quack);
echo '<img src="http://www.ngpriest.com/Time.php?month='.$quackarray['mon'] .'&day='.$quackarray['mday'].'&hour='.$quackarray['hours'].'&min=0" /><br />';

Leave a Reply

%d bloggers like this: