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 Quote Link to comment 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, Quote Link to comment 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. Quote Link to comment 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, Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.