Jump to content

Check time against system time ...?


lynxus

Recommended Posts

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

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,

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.

:(

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,

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.