PHP Check if current time is less than a certain time

Let's say I got this time 21:07:35 now and this time in variable 21:02:37 , like this one

 <?php $current_time = "21:07:35"; $passed_time = "21:02:37"; ?> 

Now I want to check if $current_time less than 5 minutes , then echo You are online So, how can I do this in PHP?
Thanks
:)

+4
source share
4 answers

To compare the set time with the current time:

 if (strtotime($given_time) >= time()+300) echo "You are online"; 

300 is the difference in seconds you want to check. In this case, 5 minutes once 60 seconds.

If you want to compare two arbitrary times, use:

 if (strtotime($timeA) >= strtotime($timeB)+300) echo "You are online"; 

To know : this will not succeed if the time is on different dates, for example 23:58 Friday and 00:03 Saturday, since you pass the time only as a variable. You would be better off storing and comparing Unix timestamps for starters.

+5
source
 $difference = strtotime( $current_time ) - strtotime( $passed_time ); 

Now $difference has a time difference in seconds, so just divide by 60 to get the difference in minutes.

+3
source

Use the Datetime Class

 //use new DateTime('now') for current $current_time = new DateTime('2013-10-11 21:07:35'); $passed_time = new DateTime('2013-10-11 21:02:37'); $interval = $current_time->diff($passed_time); $diff = $interval->format("%i%"); if($diff < 5){ echo "online"; } 
+1
source
 $my_time = "3:25:00"; $time_diff = strtotime(strftime("%F") . ' ' .$my_time) - time(); if($time_diff < 0) printf('Time exceeded by %d seconds', -$time_diff); else printf('Another %d seconds to go', $time_diff); 
+1
source

Source: https://habr.com/ru/post/1495151/


All Articles