简体   繁体   中英

Comparing Two Time stamps in PHP

I am trying to prevent spam in my contact form. When anyone contact using contact form, I am saving his IP Address in Database with Current Time. If someone try contact, I am checking IP in database and if there IP address entry in Database, I am getting timestamp of it and comparing it with current timestamp but I think there something wrong with it and its giving me wrong time and so user are still able to send messages.

I have defined TimeZone like below

define('TIMEZONE', 'Asia/Kolkata');
date_default_timezone_set(TIMEZONE);
$date = date('Y/m/d h:i:s', time());

I am inserting write time in database same as echo in php.

Now I am getting and comparing both time like below

$last_time = $row['time'];
$current_time = $date;

    if(($current_time-strtotime($last_time )) > 1800) {
    //send mail
    }
    else {
    // give error
    }

I have tried echo both time and I am getting result like this

$last_time = 2018-09-23 07:56:37
$current_time = 2018/09/23 07:56:51
($current_time-strtotime($last_time ) = -1537667579

I don't know whats wrong with it.

Let me know if there anything wrong with it.

Also convert the current time into strtotime. Make sure both date format should be same

define('TIMEZONE', 'Asia/Kolkata');
date_default_timezone_set(TIMEZONE);
$date = date('Y/m/d h:i:s', time());

$last_time = $row['time'];
$current_time = $date;

if((strtotime($current_time)-strtotime($last_time )) > 1800) {
//send mail
}
else {
// give error
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM