Reputation: 1262
I'm trying to compare 2 dates with the usual code.
$date1 = date("d/m/Y",strtotime($row['ad_date']));
$date = $_GET['date'];
if($date1 > strtotime($date)){
echo ("Yes");
}else{
echo ("No");
}
The row['ad_date']
has "Y-m-d" format and the $_GET['date']
has "d-m-Y" format.
PHP prints YES only when the MONTH of the $_GET['date']
is lower than the DAY of $row['date']
When I echo both, after all the changes of the format, the format seems the same:
13/12/2013 (date1) - 01/12/2013(date)
The $_GET['date']
is from the calendar of the ui.js
jQuery and the $row['date']
from mysql.
Any idea guys?
Upvotes: 1
Views: 119
Reputation: 1290
You are doing straight string comparison, you should use strtotime to parse the date string to epoch time so you can compare them with numerical comparison.
$date1 = strtotime($row['ad_date']);
$date = strtotime($_GET['date']);
echo "{$row['ad_date']} -> {$date1}<br>";
echo "{$_GET['date']} -> {$date}<br>";
if($date1 > $date){
echo ("Yes");
}else{
echo ("No");
}
Upvotes: 4
Reputation: 219804
This is a lot easier using DateTime
objects as they are comparable:
$date1 = new DateTime($row['ad_date']);
$date2 = DateTime::CreateFromFormat("d-m-Y", $_GET['date']);
if($date1 > $date2){
echo ("Yes");
}else{
echo ("No");
}
Upvotes: 2