I need to have a validation that checks if the "last_update" is done before 3 hours or less.
I have this PHP code:
if($user['last_update'] < strtotime("3 hours")) {
$msg .= "You can only register every 3 hour.";
}
How could this be done?
If $user['last_update']
is in date format, then
if (time() - strtotime($user['last_update']) < 3 * 3600) {
//..
}
Try like this. Wrap your $user['last_update']
inside strtotime()
.
if(strtotime($user['last_update']) < strtotime("3 hours")) {
$msg .= "You can only register every 3 hour.";
}
You can use this to get the current time minus 3 hours:
$dateTime = new DateTime();
$dateTime->modify("-3 hours");
$time = $dateTime->format("H:m:s");
$date = $dateTime->format("Y-m-d");
You can then compare the date and time variables with your last update.
strtotime()
returns seconds.
If your date is stored as UNIX timestamp, then your code is correct. If your date is stored as TIMESTAMP, following will be the code:
$three_hours = date('Y-m-d H:i:s', strtotime('3 hours'));
if ($user['last_update'] < $three_hours) {
$msg .= "You can only register every 3 hour.";
}