Jump to content

Comparing timestamp to date....


cgm225

Recommended Posts

I have a timestamp in the following format::  0000-00-00 00:00:00  and I want to check it against the time() function and dermine if the timestamp is less than a minute old.  I have been trying to do this without success.  So for I have the following...

 

        if (empty($timestamp)) {
            $timestamp = 0;
        } else {
            $timestamp = strtotime($timestamp);
        }

        //New comments limited to every sixty seconds
        if((time() - $timestamp) < (60)) {
            $this->successfulSubmission = false;
            return false;
        } else {
            return true;  
        }

 

Any ideas what I am doing wrong?

Link to comment
https://forums.phpfreaks.com/topic/124006-comparing-timestamp-to-date/
Share on other sites

Where is your original timestamp coming from? If it is a database, it is much easier to just select records in the query that have a timestamp with the values you are looking for.

 

What have you done to troubleshoot the problem? Post an actual value in $timestamp that you are passing to the code.

I get the timestamp value from a database as follows:

 

        $query = 'SELECT id, ip_address, timestamp FROM comments WHERE ip_address =
            ? ORDER BY timestamp ASC';
        $statement = $this->connection->prepare($query);
        $statement->bind_param('s', $this->ip); 
        $statement->bind_result($id, $ip_address, $timestamp);
        $statement->execute();
        $statement->fetch();
        $statement->close();

 

 

 

Also, I have tried the following also to just see what the output looks like, and I get the following

 

$timestamp = time();
echo (time() - $timestamp);  //Output is 0 which makes sense

/////////

$timestamp = '2008-08-25 20:48:48';
echo time();   //Output is 1221310409
echo (time() - $timestamp);   //Output is 1221308401 
echo (time() - strtotime($timestamp));   //Output is 1598681

 

So I am not sure what exactly I am doing wrong?  Again, my overall goal is to find out if a timestamp is less than a minute old (to prevent a user from posting again).

 

Thanks again for the help!

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.