Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Check if the current time is less than a specific time

Tags:

php

time

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

like image 511
AFB Avatar asked Aug 04 '13 01:08

AFB


2 Answers

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.

like image 130
ironcito Avatar answered Nov 09 '22 12:11

ironcito


$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.

like image 29
Nick Coons Avatar answered Nov 09 '22 12:11

Nick Coons