How can I make a script that can check wether it is currently x seconds fro开发者_如何转开发m 12am or 12pm?
thanks
You have to get the current timestamp, using the time()
function.
Then, you have to get the timestamp of 12am, using for example the strtotime()
function.
Then, substract those two values ; and if the absolute value of the result is X
, then it's the right time for you ;-)
<?php
$noon = strtotime( "noon" );
$midnight = strtotime( "midnight" );
$timeToMidnight = $midnight - date();
$timeToNoon = $noon - time();
?>
<?php
function timeDiff($firstTime,$lastTime)
{
// convert to unix timestamps
$firstTime=strtotime($firstTime);
$lastTime=strtotime($lastTime);
// perform subtraction to get the difference (in seconds) between times
$timeDiff=$lastTime-$firstTime;
// return the difference
return $timeDiff;
}
//Usage :
echo timeDiff("2002-04-16 10:00:00","2002-03-16 18:56:32");
?>
<?php
$time = time();
if( (date("g", $time) % 12) * 3600
+ preg_replace('/^0/', '', date("i", $time)) * 60
+ preg_replace('/^0/', '', date("s", $time))
> $selected_time) {
// do whatever
}
?>
This should work... right?
$time = time(); if ((date("g", $time) == 12) && (date("i", $time) < 10)) {echo 'W00T!';}
精彩评论