How do I check if a date is more than 3 months old?
Moderator: General Moderators
-
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?
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.
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.
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?
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.
All the best from the United Kingdom.
Re: How do I check if a date is more than 3 months old?
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?
Great thanks. 
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: How do I check if a date is more than 3 months old?
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?
How would I query if the date in the mySQL db is less than 30 days old?
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?
Code: Select all
$resultuk = mysql_query ("SELECT romancode, photoprimary, photo FROM products WHERE datelastupdated < 30days old ) or die(mysql_error());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.
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?
Code: Select all
SELECT romancode, photoprimary, photo FROM products WHERE DATEDIFF(NOW(), datelastupdated) < 30Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: How do I check if a date is more than 3 months old?
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]
[text]DATE_SUB(curdate(), INTERVAL 1 MONTH)[/text]