PHP - Comparing time

江枫思渺然 提交于 2019-12-01 01:18:53

If those times are in the database, comparison operator of the database would works. For example:

SELECT * FROM table WHERE time < NOW()

In PHP, the easiest way to compare times is to convert them to timestamps, and then to compare timestamps as integers. You can use strtotime to do that conversion.

For example:

$time1 = "08:00:00";
$time2 = "09:00:00";

if (strtotime($time1) > strtotime($time2) ||
    strtotime($time1) < strtotime("08:00:00")) {
   ...
}

If you're running PHP 5.3, you can use the diff() method of DateTime objects to get the difference in between two dates. But it's possible to do with just timestamps too (1 day = 86400 seconds)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!