Xavier
Xavier

Reputation: 41

PHP: If $timestamp is older than 12 weeks then…

echo ($timestamp) gives "2012-05-03 15:35:46"

What would a PHP command look like that does:"if $timestamp is older than 12 weeks then…"

Thanks for any help!

Upvotes: 1

Views: 6287

Answers (4)

David Bélanger
David Bélanger

Reputation: 7438

This is the fastest way of doing it in PHP in term of micro optimisation.

if(strtotime($timestamp) <= time() - (60*60*24*7*12)){
    // 60 secs * 60 mins * 24 hours * 7 days * 12 weeks
}

Please go read about strtotime to know how to convert timestamp to unixtime. Also read about time function.

Upvotes: 16

Davide Gualano
Davide Gualano

Reputation: 12993

If you are using php > 5.2, you can use DateTime and DateInterval objects:

$now = new DateTime();
$before = bew DateTime("2012-05-03 15:35:46");

$diff = $now->diff($before);

if ($diff->d > (12 * 7)) {
    print "Older than 12 weeks";
}

See here the documentation of the above objects.

Upvotes: 0

John Conde
John Conde

Reputation: 219864

$time_to_compare  = new DateTime();
$twelve_weeks_ago = new DateTime ("-12 weeks");
if ($time_to_compare < $twelve_weeks_ago)
{
    // Do stuff
}

Upvotes: 0

Dunhamzzz
Dunhamzzz

Reputation: 14808

What you need is strtotime()!

$timestamp = strtotime($timestamp);
$twelveWeeksAgo = strtotime('-12 weeks');

if($timestamp < $twelveWeeksAgo) {
 // do something
}

There are multiple ways of checking this, this is the most explanatory at a glance IMHO.

Upvotes: 6

Related Questions