How do I check if a date is more than 3 months old?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

How do I check if a date is more than 3 months old?

Post by simonmlewis »

I have a date: $row->datesubmitted.
I need to test if based on today's date, if it is more than 3 months old. And if it is, I have a script to perform.

I've seen how to do this in a MySQL statement, but not in a PHP script itself. And rather strangely, cannot find any method elsewhere.

I'm sure it's simple.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I check if a date is more than 3 months old?

Post by simonmlewis »

Bingo.

Code: Select all

$date1 = strtotime($row->datesubmitted);
$date2 = strtotime($today);
$months = 0;

while (($date1 = strtotime('+1 MONTH', $date1)) <= $date2)
    $months++;

echo $months;
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: How do I check if a date is more than 3 months old?

Post by Weirdan »

Code: Select all

$threeMonthAgo = new DateTime("-3 month");
$then = new DateTime($row->dateSubmitted);
if ($then < $threeMonthAgo) {
   // do something
}
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I check if a date is more than 3 months old?

Post by simonmlewis »

Great thanks. :)
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: How do I check if a date is more than 3 months old?

Post by pickle »

My turn to take a swing in this golf game:

Code: Select all

if($row->dateSubmitted < strtotime('-3 month'){
  // do something
}
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I check if a date is more than 3 months old?

Post by simonmlewis »

How would I query if the date in the mySQL db is less than 30 days old?

Code: Select all

$resultuk = mysql_query ("SELECT romancode, photoprimary, photo FROM products WHERE datelastupdated < 30days old ) or die(mysql_error());
This kind of thing.
I guess it needs to check against the current date, and see if the difference is less than 30 days - but how would I do that within a query?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I check if a date is more than 3 months old?

Post by simonmlewis »

Code: Select all

SELECT romancode, photoprimary, photo FROM products WHERE DATEDIFF(NOW(), datelastupdated) < 30
Would this work??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: How do I check if a date is more than 3 months old?

Post by Eric! »

That should give you less than 30 days. If you want to make SQL do the work for dates too, you can generate datelastupdated with date_sub.
[text]DATE_SUB(curdate(), INTERVAL 1 MONTH)[/text]
Post Reply