PHP - 比较时间 [英] PHP - Comparing time

查看:93
本文介绍了PHP - 比较时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用日历/计划器网络应用程序,我需要比较事件的开始和结束时间,然后将其存储在我的数据库中。事件只能有一天的范围,并在8am和午夜之间。开始时间始终必须在结束时间之前发生。

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 and between 8am and midnight. The start time always has to take place before the end time.

帖子值来自以下格式的表单 hh:mm:ss 12:14:00 )等等。所以我可以将它们存储在我的数据库中没有太多的麻烦。

The post values come from the form in the following format hh:mm:ss (12:14:00) etc.. so I can store them in my database without much hassle. Is there any way I can compare these times?

非常感谢。

推荐答案

如果这些时间在数据库中,数据库的比较运算符将工作。例如:

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

SELECT * FROM table WHERE time < NOW()

在PHP中,比较时间最简单的方法是将它们转换为时间戳,然后以将时间戳作为整数进行比较。您可以使用 strtotime 进行转换。

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.

例如:

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

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

这篇关于PHP - 比较时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆