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
Asked
Active
Viewed 3.4k times
20

Edoardo
- 599
- 2
- 9
- 26
-
Is the string variable a date? – Blinkydamo Nov 11 '16 at 12:11
-
2Possible duplicate of [PHP compare two dates](http://stackoverflow.com/questions/20008581/php-compare-two-dates) – Ruslan Osmanov Nov 11 '16 at 12:13
2 Answers
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
-
2yes `$time1` and `$time2` are integers, so everything which works on integers will work on those. – Abhay Maurya Nov 11 '16 at 12:46
-
1But use `===` instead as its faster than `==` and more appropriate. Please mark the answer as accepted for this question if it helped you. – Abhay Maurya Nov 11 '16 at 12:47
-
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