Reputation: 4995
I have the timestamp formatted in the regular way like this:
2012-02-23 20:34:55
I need to detect the following thing.
if(Timestamp >= 1 month ago) {
}
if(Timestamp >= 5 minutes ago) {
}
How would you detect that type of thing from a timestamp variable in PHP?
By the >=
I mean if the time stamp has been at least or longer than 5 minutes ago.
Upvotes: 1
Views: 559
Reputation: 164980
First, create a DateTime
object from your time string and one for now
$dt = new DateTime($timestamp);
$now = new DateTime;
Then, create some DateInterval objects
$oneMonth = new DateInterval('P1M');
$fiveMinutes = new DateInterval('PT5M');
Then, do your comparisons
if ($dt >= $now->sub($oneMonth)) {
}
if ($dt >= $now->sub($fiveMinutes)) {
}
Upvotes: 3