Farhana Farhana - 8 days ago 6
PHP Question

strtotime is not correctly working for -60 days

I am trying to compare two dates, but looks to me for some reason using -60 days is not working for strtotime.

$date = date('Y-m-d H:i:s', strtotime("-60 days")); // gives me string) 2016-09-25 09:27:26
$today_dt = strtotime($date);
$expire_dt = strtotime($result['insertedInvoice']); // insertedInvoice shows 2016-11-24 08:56:48

if( $today_dt > $expire_dt){
dothis();
}else{
dothat();
}


The problem is that in this function it should go for the
dothis();
but instead its going for
dothat();
Since $today_dt is like 60 days behind, it cant be greater than $expire_dt.

Answer

Try following,

$now = date('Y-m-d H:i:s');

$date =  date('Y-m-d H:i:s', strtotime('-60 days', strtotime($now)));
$today_dt = strtotime($date);
$expire_dt = strtotime($result['insertedInvoice']); // insertedInvoice shows 2016-11-24 08:56:48

if( $today_dt > $expire_dt){
 dothis();
}else{
  dothat();
}

** I write this code as per your code, please check if condition. If needed change if condition as follows (I am little bit confused with your last sentence)

if( $today_dt < $expire_dt){
   dothis();
} else {
   dothat();
}
Comments