lynxus Posted June 25, 2010 Share Posted June 25, 2010 Hi Guys, I have a script that has a date stamp like $datetime = "Jun 25 15:40:54" How can i check this against the system time and only do stuff if its in the last 2 minutes? Ie: if ($datetime "is in the last 2 minutes") { do stuff; } Info: Its a linux server if that helps. Any help here would be great. Thanks G Link to comment https://forums.phpfreaks.com/topic/205852-check-time-against-system-time/ Share on other sites More sharing options...
bluejay002 Posted June 25, 2010 Share Posted June 25, 2010 Well, it doesn't matter much what server since PHP will do it for you. So for the system time, which I suppose you mean the server where PHP was installed, you can use date() function. See it here for more details: http://php.net/manual/en/function.date.php bluejay, Link to comment https://forums.phpfreaks.com/topic/205852-check-time-against-system-time/#findComment-1077172 Share on other sites More sharing options...
lynxus Posted June 25, 2010 Author Share Posted June 25, 2010 Well, it doesn't matter much what server since PHP will do it for you. So for the system time, which I suppose you mean the server where PHP was installed, you can use date() function. See it here for more details: http://php.net/manual/en/function.date.php bluejay, Cool, This is where my PHP goes bad. I really cant get to grips with the date function and matching it against a date thats in a var. Link to comment https://forums.phpfreaks.com/topic/205852-check-time-against-system-time/#findComment-1077199 Share on other sites More sharing options...
bluejay002 Posted June 25, 2010 Share Posted June 25, 2010 If you are to compare a date with the current date, do this: $currentTime = mktime(); $userTime = strtotime(place_here_valid_date_time_from_variable); // replace that value if(($currentTime - $userTime) >= 120) { // do whatever you like } Hope this helps. bluejay, Link to comment https://forums.phpfreaks.com/topic/205852-check-time-against-system-time/#findComment-1077201 Share on other sites More sharing options...
kenrbnsn Posted June 25, 2010 Share Posted June 25, 2010 Here's something off the top of my head: <?php $datetime = "Jun 25 15:40:54"; $now = time(); $now_minus2 = $now - 120; $now_plus2 = $now + 120; $comp = strtotime($datetime); if ($comp >= $now_minus2 && $comp <= $now_plus2) { echo "$datetime is within range<br>\n"; } else { echo "not within range<br>\n"; } ?> Ken Link to comment https://forums.phpfreaks.com/topic/205852-check-time-against-system-time/#findComment-1077202 Share on other sites More sharing options...
lynxus Posted June 25, 2010 Author Share Posted June 25, 2010 Here's something off the top of my head: <?php $datetime = "Jun 25 15:40:54"; $now = time(); $now_minus2 = $now - 120; $now_plus2 = $now + 120; $comp = strtotime($datetime); if ($comp >= $now_minus2 && $comp <= $now_plus2) { echo "$datetime is within range<br>\n"; } else { echo "not within range<br>\n"; } ?> Ken Thanks man, That seems to work Thanks again. Link to comment https://forums.phpfreaks.com/topic/205852-check-time-against-system-time/#findComment-1077207 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.