Let's say I got this time 21:07:35
now and this time into a variable 21:02:37
like this
<?php
$current_time = "21:07:35";
$passed_time = "21:02:37";
?>
Now I want check if $current_time
is less than 5 minutes
then echo You are online
So how can I do this in PHP?
Thanks
:)
To compare a given time to the current time:
if (strtotime($given_time) >= time()+300) echo "You are online";
300 is the difference in seconds that you want to check. In this case, 5 minutes times 60 seconds.
If you want to compare two arbitrary times, use:
if (strtotime($timeA) >= strtotime($timeB)+300) echo "You are online";
Be aware: this will fail if the times are on different dates, such as 23:58 Friday and 00:03 Saturday, since you're only passing the time as a variable. You'd be better off storing and comparing the Unix timestamps to begin with.
$difference = strtotime( $current_time ) - strtotime( $passed_time );
Now $difference
holds the difference in time in seconds, so just divide by 60 to get the difference in minutes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With