Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timestamp check if older than 12 hours [closed]

Tags:

php

timestamp

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.

like image 263
Mark Topper Avatar asked Jan 12 '13 23:01

Mark Topper


2 Answers

$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
}
like image 77
0xJoKe Avatar answered Oct 19 '22 07:10

0xJoKe


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.

like image 36
Nemo64 Avatar answered Oct 19 '22 07:10

Nemo64