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

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

问题描述

我正在开发一个日历/计划器网络应用程序,我需要比较事件的开始时间和结束时间,然后再将它们存储到我的数据库中.一个事件的范围只能是一天以及上午 8 点到午夜之间.开始时间总是必须在结束时间之前.

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.

post 值来自以下格式的表单 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天全站免登陆