16

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
  • 549
  • 2
  • 7
  • 25

2 Answers2

29

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
  • 10,668
  • 6
  • 42
  • 58
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
  • 6,650
  • 2
  • 16
  • 36
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