Page 1 of 1
finding time between now and any given time
Posted: Tue Oct 12, 2004 2:11 pm
by josh
For a website i need to check if an event is going to happen, or happened within the past or upcoming 10 minutes so i need to be able to look at a date and say that is 5 minutes from now, or that time was 10 minutes ago for example...
As of now i have the following code
Code: Select all
<?php
$dtime = "1:07:23"; // Example time
list($h, $i, $s) = split(':', $dtime);
$h = $h - date('h');
$i -= date('i');
$s -= date('s');
echo ('Time of event ' . $dtime . '<BR>Current time ' . date('h:i:s') . '<BR>The time between items ' . $h . ":" . $i . ":" . $s);
echo "<BR><BR>It is currently $h hours, $i minutes, and $s seconds before";
?>
but that will not work because if one time is before noon and the other time is after noon it will read out the wrong information
Posted: Tue Oct 12, 2004 2:44 pm
by nigma
If you could store the time of the event using the [php_man]time()[/php_man] function it would be pretty easy to do this.
For example, say $eventTime contains a unix timestamp for the time of an event and $currentTime contains a unix timestamp for the current time. You could add/subtract 10minutes to $currentTime and then compare that time to the $eventTime to find out if the event has already happend or will happen in the future.
You use the [php_man]date()[/php_man] function to then create an easily readible version of the timestamp for display.
Reply if you need clearification.
Posted: Tue Oct 12, 2004 3:50 pm
by josh
I have no experience with unix timestamps or with time manipulation in php... could you show me code to return the minutes +/- between the event and current time? The problem i have is i dont have it specified AM or PM so 1:00 shows up as 11 hours before 12:00 when it should show up as 1 hour after... what is a good way to store the PM/AM and to compare it and all?
Posted: Tue Oct 12, 2004 7:44 pm
by nigma
jshpro2 wrote:could you show me code to return the minutes +/- between the event and current time?
Assuming that both $eventTime and $currentTime are valid unix timestamps:
Code: Select all
// To get difference between the time of the event and the current time:
$difference = abs($currentTime - $eventTime);
// To format the difference into a more easily readable format:
$readableFormat = date("h:i:s a",$difference); // displays something like hh:mm:ss am/pm
The [php_man]abs()[/php_man] function takes the absolute value of a number. In this case it would take the absolute value of $currentTime - $eventTime. We do this so that if the event is in the future then you don't get a negative number.
jshpro2 wrote:what is a good way to store the PM/AM and to compare it and all?
Using unix timestamps

You don't have to do anything special to specify am/pm when using time() because it takes care of that.