20

i'm developing a PHP program and i must compare a date variable and a string variable. I've tried strcmp but it doesn't work... suggests? Thank's

Edoardo
  • 599
  • 2
  • 9
  • 26

2 Answers2

35

Best way of comparing dates is using time-stamps :

$string = '11/05/2016';//string variable
$date = date('Y-m-d',time());//date variable

$time1 = strtotime($string);
$time2 = strtotime($date);
if($time1>$time2){
    //do this
}
else{
    //do this
}

I hope it helps

Abhay Maurya
  • 11,819
  • 8
  • 46
  • 64
1
$time = strtotime('10/16/2003');

$newformat = date('Y-m-d',$time); //my date format is Y-m-d,usw your date format

there $newformat variable also a date

this way you can compare date and string

Passionate Coder
  • 7,154
  • 2
  • 19
  • 44
Ronak
  • 36
  • 5
  • This answer is only partial. While it shows how to prepare the string for comparison with a date, it doesn't show how to do the actual comparison, but the question was "how to compare." – Dylan Kinnett Jun 07 '19 at 14:05