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

2
  • Is the string variable a date? Commented Nov 11, 2016 at 12:11
  • 2
    Possible duplicate of PHP compare two dates Commented Nov 11, 2016 at 12:13

2 Answers 2

39

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

Sign up to request clarification or add additional context in comments.

3 Comments

yes $time1 and $time2 are integers, so everything which works on integers will work on those.
But use === instead as its faster than == and more appropriate. Please mark the answer as accepted for this question if it helped you.
No Problem. Happy Coding!
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

1 Comment

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."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.