finding time between now and any given time

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

finding time between now and any given time

Post 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
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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?
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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.
Post Reply