Page 1 of 1
How do I check if a date is more than 3 months old?
Posted: Wed Feb 06, 2013 9:26 am
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.
Re: How do I check if a date is more than 3 months old?
Posted: Wed Feb 06, 2013 9:49 am
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;
Re: How do I check if a date is more than 3 months old?
Posted: Wed Feb 06, 2013 11:00 am
by Weirdan
Code: Select all
$threeMonthAgo = new DateTime("-3 month");
$then = new DateTime($row->dateSubmitted);
if ($then < $threeMonthAgo) {
// do something
}
Re: How do I check if a date is more than 3 months old?
Posted: Wed Feb 06, 2013 11:03 am
by simonmlewis
Great thanks.

Re: How do I check if a date is more than 3 months old?
Posted: Wed Feb 06, 2013 12:16 pm
by pickle
My turn to take a swing in this golf game:
Code: Select all
if($row->dateSubmitted < strtotime('-3 month'){
// do something
}
Re: How do I check if a date is more than 3 months old?
Posted: Mon Jun 03, 2013 8:23 am
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?
Re: How do I check if a date is more than 3 months old?
Posted: Mon Jun 03, 2013 8:25 am
by simonmlewis
Code: Select all
SELECT romancode, photoprimary, photo FROM products WHERE DATEDIFF(NOW(), datelastupdated) < 30
Would this work??
Re: How do I check if a date is more than 3 months old?
Posted: Mon Jun 03, 2013 11:43 am
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]