Time based script

Hi,

I’m trying to put together a script that would basically display a message between 8-10pm and otherwise display nothing else. This logic below seems to make sense to me but it doesn’t appear to be working - I’m new to PHP and I’m sure anybody else can see the obvious mistakes, so please point them out to me!

Thank you!

[php] <?php

$b = time();

$hour = date(“g”,$b);
$m = date (“A”, $b);

if ($m == “PM”)
{
if ($hour == 12)
{
echo “”;
}
elseif ($hour == 8)
{
echo “

We are OPEN LATE

So call us TONIGHT
”;
}
elseif ($hour < 10)
{
echo “
We are OPEN LATE

So call us TONIGHT
”;
}
elseif ($hour > 9)
{
echo “”;
}
elseif ($m == “AM”)
{
echo “”;
}
}

?>[/php]

use the 24 hour clock, and since you’re using strings for comparison, use strtotime().
[php]
$hour = strtotime(date(‘G’), time()); // format the date for straight comparison
[/php]

Since its now a 24 hour clock, the am and pm comparisons aren’t needed, simplifying the code a bit more. The html could use some work too, but that’s for another thread i guess :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service