Check if the time has passed since the date more than X days

I need to create a PHP script that pulls timestamps from different stuff from the database (logs, messages, logins, etc.) and removes them if they are older than X days. I am poor at dealing with time and I am a little obsessed with how to do this.

I realize I can split the day / month / year on a string using explode () and compare them to a bunch of If statements, but would like to use a more efficient method. Something like the following would be the correct way to do it right?

$dt = "2011-03-19 10:05:44";

//if $dt is older than 90 days
if((time()-(60*24*90)) > strtotime($dt))
{

}

      

Subtract (minutes * hours * days) from time () or are the numbers wrong?

+3


source to share


1 answer


You can use the class for this DateTime

. Example:



$dt = "2011-03-19 10:05:44";
$date = new DateTime($dt);
$now = new DateTime();
$diff = $now->diff($date);
if($diff->days > 90) {
    echo 'its greater than 90 days';
}

      

+6


source







All Articles