Reputation: 769
I have a date I'm pulling from my database. I want to check if that date is over a year old.
I have done the following but I'm sure my logic is incorrect.
if (strtotime($horsesplaced1['Date']) < strtotime('-1 year')) {
//true
$placed .= "/";
$stopyear = "yes";
}
Upvotes: 6
Views: 11142
Reputation: 3425
You just need to compare using timestamp here.
Do like this:
if(strtotime($your_date) < strtotime('-1 year') ){
echo "Date is older than year";
}else{
echo "Date is not older than year";
}
$your_date
should be in datetime
format.
Upvotes: 1
Reputation: 3868
Try like this:
<?
$dbDate = '2014-01-20 17:14:40';
if(strtotime($dbDate)<strtotime('-1 year')){
echo "YES";
}else{
echo "NOP";
}
?>
Upvotes: 11
Reputation: 3242
PHP DateTime is far more useful for this. Something like:
$new = new DateTime('2015-01-01');
$old = new DateTime('2013-01-01');
if ( $old->modify('+1 year') < $new) {
echo "More than a year ago";
}
Upvotes: 6
Reputation: 27295
With the DateTime functions its much easier to solve such problems.
$checkDate = new \DateTime('2013-01-01');
$pastDate = clone $aktDate;
$pastDate->modify('-1 year');
if($checkDate < $pastDate) {
// Do something
}
I don't know if you work with a datetime field/object. If you have a datetime object you can work directly with them.
Upvotes: 1