Web Hosting Talk







View Full Version : PHP Script to Check Server Time


gamemaster
01-15-2005, 03:50 PM
Greetings. Can someone help me with a bit of PHP code that I can place on a page that checks the server time and if the time is between 'x' and 'y' then send to location?

For instance:


$url = "www.domain.com";

if (server time is 2100 hrs to 2300 hrs ) {
@header("Location: ".$url);
}


Thanks much.

error404
01-15-2005, 03:54 PM
$url = "www.domain.com";

$curtime = time();
if ($curtime > strtotime("21:00") && $curtime < strtotime("23:00"))
@header("Location: " . $url);

gamemaster
01-15-2005, 04:14 PM
if (!$curtime > strtotime("21:00") && $curtime < strtotime("23:00"))

Thanks for the quick reply. I tested with the '!' as above and it does not seem to work. What could be wrong?

error404
01-15-2005, 09:34 PM
You just need to put brackets around the expression you're negating. The ! operator has top precendence (I believe), so it applies only to what's directly after it, $curtime. It returns a boolean value, which is then coerced into an integer for the > operator. The left side will be either 0 or 1, which will never be greater than the result of strtotime. Instead, you need to force the > operation to execute first, then negate that:

This means 'If the current time is not after 9pm today AND the current time is before 11pm today, then include the file'. You could achieve this with only the first condition (as the second condition will always be true if the first one is), so I'm not sure this is what you're going for.


$url = "www.domain.com";

$curtime = time();
if (!($curtime > strtotime("21:00")) && $curtime < strtotime("23:00"))
@header("Location: " . $url);

gamemaster
01-15-2005, 09:42 PM
Thank you very much for both the explanation and code - it works.

gamemaster
01-16-2005, 12:14 AM
Okay, after some more testing I noticed it did not work, what else could be wrong?

gamemaster
01-16-2005, 09:13 PM
I know what the problem could be... I ran this test:

<?
$curtime = time();
print "$curtime";
?>

and got this response:

1105924262

How do I interpret the above?

error404
01-16-2005, 09:45 PM
Originally posted by gamemaster
I know what the problem could be... I ran this test:

<?
$curtime = time();
print "$curtime";
?>

and got this response:

1105924262

How do I interpret the above?

It's a unix timestamp. The number of seconds since January 1 1970 00:00:00 GMT. The strtotime() function also returns a unix timestamp, so comparisons should be simple.

gamemaster
01-16-2005, 09:50 PM
So how does that translate into the code you give me so that it checks the times mentioned?

error404
01-16-2005, 10:14 PM
$url = "www.domain.com";

$curtime = time(); // set to the current timestamp, let's use 6:00pm gmt jan 1 1970 as an example - 64800 seconds after midnight, so $curtime is now 64800
if (
!($curtime > strtotime("21:00")) && // strtotime() gives you a timestamp for the time you give it..so we get 75600 in this example. 6:00pm is not greater than 9:00pm, so this should fail, and does because 64800 is less than 75600. Then we negate it with ! so this condition is true
$curtime < strtotime("23:00")) // same idea here, 23:00 is 82800 this time, and 75600 is indeed less than this (earlier than 11pm)
@header("Location: " . $url); // Both conditions are true, so we'd get here in this case

gamemaster
01-16-2005, 10:26 PM
Thanks for the specific info, but doesn't it need more than simply 'strtotime("21:00")' to work since it is running off of a unix time stamp?

UPDATE: No it doesn't (to answer my own question).

I am using this as part of another script so I guess I will need to see why this does not work within it. - thanks.