How can I check if a timestamp is older than 12 hours?
I need to make a link work for 12 hours, after that I need to display an error message.
$dateFromDatabase = strtotime("2012-12-04 12:05:04");
$dateTwelveHoursAgo = strtotime("-12 hours");
if ($dateFromDatabase >= $dateTwelveHoursAgo) {
// less than 12 hours ago
}
else {
// more than 12 hours ago
}
I guess you mean a unix-timestamp. If so you can apply simple math:
// a unix-timestamp is the time since 1970 in seconds
// 60 seconds = 1 minute, 60 minutes = 1 hour, 12 hours
// so this calculates the current timestamp - 12 hours and checks
// if 12 hours ago the timestamp from your database lay in the future
if ( $timestamp > (time() - 60*60*12) ) {
// show your link
} else {
// 12 hours are up
// give your error message
}
If your database returns a formatted date (like mysql with the timestamp/date field types) you have to use strtotime()
to get a unix timestamp first.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With