PHP - Comparing time

后端 未结 2 883
迷失自我
迷失自我 2021-01-07 02:56

I\'m working on a calendar/planner web app and I need to compare the start and end times of events before I store them in my DB. An event can only have a range of one day an

2条回答
  •  再見小時候
    2021-01-07 03:22

    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")) {
       ...
    }
    

提交回复
热议问题