Ella
Ella

Reputation: 921

find if date is older than 30 days

The date string looks like this

2011-08-19 17:14:40

(year-month-day hours:minutes:seconds)

How can I find out if the date is older than the current date with more than 30 days?

Upvotes: 91

Views: 109528

Answers (4)

Collin Krawll
Collin Krawll

Reputation: 2520

If you are on PHP 5.3 or higher you can do:

$someDate = new \DateTime('2011-08-19 17:14:40');
$now = new \DateTime();

if($someDate->diff($now)->days > 30) {
   echo 'The date was more than 30 days ago.';
}

Upvotes: 61

RiaD
RiaD

Reputation: 47640

Try using something like this:

 if(strtotime('2011-08-19 17:14:40') < strtotime('-30 days')) {
     // this is true
 }

Besides, this string looks like it is stored in SQL as datetime/timestamp field. You can directly select all entries from your database with old dates using:

SELECT ... WHERE `datetime` + INTERVAL 30 DAY < NOW()

Upvotes: 196

Shriganesh Shintre
Shriganesh Shintre

Reputation: 2498

You can use Carbon as follows

if (30 - ((new \Carbon\Carbon($given_date, 'UTC'))->diffInDays()) < 0) {
    echo "The date is older than 30 days";
}

Upvotes: 8

gion_13
gion_13

Reputation: 41533

strtotime('2011-08-19 17:14:40') + 30 * 24 * 60 * 60 < time();

Upvotes: 1

Related Questions