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";
}
Try like this:
<?
$dbDate = '2014-01-20 17:14:40';
if(strtotime($dbDate)<strtotime('-1 year')){
echo "YES";
}else{
echo "NOP";
}
?>
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";
}
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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.