tags:

views:

38

answers:

5

How can I make a script that can check wether it is currently x seconds from 12am or 12pm?

thanks

+5  A: 

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 ;-)

Pascal MARTIN
Ah, you're of the "teach a man to fish" school :) good call
hookedonwinter
A: 
<?php
    $noon = strtotime( "noon" );
    $midnight = strtotime( "midnight" );

    $timeToMidnight = $midnight - date();
    $timeToNoon = $noon - time();
?>
hookedonwinter
A: 
<?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");

?> 
Artem Barger
A: 
<?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

}
?>
bluesmoon
A: 

This should work... right?

$time = time(); if ((date("g", $time) == 12) && (date("i", $time) < 10)) {echo 'W00T!';}

Jigs