TSpinVirus
TSpinVirus

Reputation: 41

What's wrong with my PHP datetime code?

I'm not experienced with PHP. I'm loving it so far, except that this bit of code is working sometimes and other times not at all.

I'm trying to redirect based on the time of day. I have this script set up on multiple domains, with different times for each. Sometimes it works correctly, and sometimes it does not. I have modifed my php.ini file as well as .htaccess to try and set the timezone. I have tried writing the time in CST which my server is in(Hostgator), but no luck there either. I should note that there is some HTML that follows my code here, but it's outside of the php.

It doesn't make sense to me why it only works SOMETIMES. I've tried clearing my browser cache many times but it never helped. In this code, it should only display the html below it between 5:47 PM and 6:43 PM and redirect at all other times, right? Is it some sort of caching issue? Is the script being stored and then not being executed when I visit multiple sites in a row? Any help is greatly appreciated.

The best case scenario is this is only happening to me because I am visiting the sites too much to make sure the script is working correctly. Is that possible?

<?php
date_default_timezone_set('America/Los_Angeles');
$hour = date("G");
$min = date("i");

if(($hour >= "0" and $min >= "00") and ($hour <="17" and $min <= "47"))
    {
    header('Location: http://mysite.php');
    exit();
}
elseif(($hour >= "18" and $min >= "43") and ($hour <="23" and $min <= "59"))
    {
    header('Location: http://mysite.php');
    exit();
}
?> 

Upvotes: 2

Views: 84

Answers (2)

Alejandro Iv&#225;n
Alejandro Iv&#225;n

Reputation: 4061

Try using intval() to get an actual integer and compare that:

date_default_timezone_set('America/Los_Angeles');
$hour = intval( date("G"), 10 );
$min = intval( date("i"), 10 );

if(( $hour >= 0 and $min >= 0 ) and ( $hour <= 17 and $min <= 47 ))
{
header('Location: http://mysite.php');
exit();
}
elseif (( $hour >= 18 and $min >= 43 ) and ( $hour <= 23 and $min <= 59 ))
{
header('Location: http://mysite.php');
exit();
}

What you're doing is comparing strings, which are a character-by-character comparison (their order in the ASCII table). So, for example, if you compare "13" > "10", that's correct because "1" == "1" (equals, keeps going) and then "3" > "1" (true, returns true).

But what if you compare "098" > "100"? "0" > "1" (false, returns false). Obviously it doesn't work for your purposes, since 098 = 98 is actually less than 100.

EDIT: As @DCoder correctly mentioned, the strings are first converted to numbers (integers) and then compared. I would highly recommend to make sure to convert them to numbers explicitly and then compare to avoid possible issues when dealing with strings.

Upvotes: 1

Lucky Chingi
Lucky Chingi

Reputation: 2258

Here is the shorten code, I converted the time to Military time. If the military time is less than 1747 OR greater than 1843 - it will redirect, else will display the HTML content

            date_default_timezone_set('America/Toronto');
            $hour = date("G");
            $min = date("i");
            $milTime = $hour . $min ;

            if($milTime < 1747 || $milTime > 1843)
            {
            header('Location: http://stackoverflow.com/questions/');
            echo "Redirect";
            exit();
            }
            echo "<h1>$milTime Show HTML Content</h1>";

Upvotes: 1

Related Questions