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
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,

 

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.