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!
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...
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.
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?
// 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.