PHP time is longer than today

please help what happened to my code. Does he always show that today is more than 01/02/2016? where in 2016 more than 2015.

<?php $date_now = date("m/d/Y"); $date=date_create("01/02/2016"); $date_convert = date_format($date,"m/d/Y"); if ($date_now > $date_convert) { echo 'greater than'; }else{ echo 'Less than'; } 

PS: 02/01/2016 comes from my database

+5
source share
1 answer

You do not compare dates. You are comparing strings. In the world of string comparisons, '09 / 17/2015 '> '01 / 02/2016' because '09'> '01'. You need to either set the date in a comparable string format or compare DateTime objects that are comparable.

 <?php $date_now = date("Ymd"); // this format is string comparable if ($date_now > '2016-01-02') { echo 'greater than'; }else{ echo 'Less than'; } 

Demo

or

 <?php $date_now = new DateTime(); $date2 = new DateTime("01/02/2016"); if ($date_now > $date2) { echo 'greater than'; }else{ echo 'Less than'; } 

Demo

+21
source

All Articles