In this scenario, we need to compare current date against a stored date in the database, where the latter is in the format "YYYY-MM-D" without zero-padded days.
To compare dates in PHP, there are several approaches available:
$today_dt = new DateTime("now"); $expire_dt = new DateTime($expireDate); if ($expire_dt < $today_dt) { // Do something }
$today_time = strtotime(date("Y-m-d")); $expire_time = strtotime($expireDate); if ($expire_time < $today_time) { // Do something }
$today = date("Y-m-d"); if ($today < $expireDate) { // Do something }
When comparing the dates, it's important to remember that the stored date may not have zero-padded days. As a result, simple string comparison may not provide accurate results. Therefore, using a method like the DateTime class or strtotime() is preferred.
The above is the detailed content of How Can I Accurately Compare Dates in PHP, Especially When Dealing with Non-Zero-Padded Days?. For more information, please follow other related articles on the PHP Chinese website!