0

I need to compare dates in separate years so that I can trigger an different event in a different year, however, php doesn't recognise the comparison between the dates in different years.

Below is what i'm trying to execute;

$priceSwitchDate = date('d-m-Y',strtotime('01-01-2018'));
$dateNow = date('d-m-Y'); // currently today (in 2017)

if($dateNow >= $priceSwitchDate){ echo 'date is in 2018 change prices';}
 else { echo 'date is in 2017, keep prices the same'; }

When i run the code it always seems to think that the date today (in 2017) is more than the $priceSwitchDate (in 2018).

Any ideas why this is happening?

F0XS
  • 1,271
  • 3
  • 15
  • 19
James
  • 3
  • 3
  • 5
    Stop formatting dates as d-m-Y if you want to compare them.... either compare the timestamps, or use DateTime objects that can be directly compared – Mark Baker Dec 14 '17 at 15:18
  • why not if(date('Y') === '2018') ? – azjezz Dec 14 '17 at 15:41
  • Yes Azjezz, that's what i reverted to (just comparing the years). It works but i was concerned it was a hacky way to do it. Wasn't sure if there was a more compliant way to achieve the result – James Dec 14 '17 at 15:50

0 Answers0