I have run across php code the compares dates in YYYY-mm-dd format as strings. Does this work? It seems to work in simple cases, but I am not sure it makes sense to compare them as strings.
<?php
$today = '2013-02-11';
$start_date = '2013-02-11';
$end_date = '2013-02-12';
$on_promo = (($start_date <= $today) && ($end_date >= $today));
if ($on_promo)
{
echo 'ON promo';
}
else
{
echo 'OFF promo';
}
?>
When comparing strings in PHP using greater than or less than, it compares them in alphabetical order.
Alphabetically
2013-02-10comes before2013-02-13If we have:
However, note that if the strings are both numerical, it will cast them both to integers
Therefore if using the format
YYYY-MM-DDyou can compare two dates perfectly fine, however I really don’t recommend relying on this. Someone might throw in a date like2013-2-11(note the month doesn’t have the leading0) and it will completely throw off the logic. It is much better to take John Conde’s suggestion and useDateTime