Edoardo
Edoardo

Reputation: 619

How to compare dates and strings in PHP

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

Upvotes: 20

Views: 40242

Answers (2)

Abhay Maurya
Abhay Maurya

Reputation: 12277

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

Upvotes: 38

Ronak
Ronak

Reputation: 36

$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

Upvotes: 1

Related Questions