PHP:如何将时间字符串与date('H:i')进行比较? [英] PHP: How to compare a time string with date('H:i')?

查看:362
本文介绍了PHP:如何将时间字符串与date('H:i')进行比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有7点30分这样的时间在数据库中保存为varchar字段。我想检查这个时间是否大于现在的时间。

I have time saved in database like 7:30pm as a varchar field. I want to check if this time is greater than time right now or not.

我将数据库时间字符串转换为'19:30',现在我想做些什么像这样:

I converted the DB time string into '19:30' and now I want to do something like this:

$my_time = '19:30';

if($my_time  > date('H:i'))
{
    do something ...
}

问题是,如果$ my_time为非空字符串,以上内容将始终返回true。

The problem is the above will return always true if $my_time is non-empty string.

执行 strtotime($ my_time)也不起作用。

strtotime ('H:i',$ my_time)设为00:00。

正在执行(int)date( 'H:i')在实际时间为17:09时会给出1700,因此删除冒号然后进行比较也不会起作用...。

doing (int)date('H:i') will give 1700 when the actual time is 17:09, so removing colon and then comparing will not work too ....

在这种情况下更改数据库时间数据毫无疑问。

Changing database time data is out of question in this context.

plz帮助。如果我说错了一些事实,请指正。

plz help. Correct me if I stated some facts wrong.

推荐答案

您可以使用此代码:

$myTime = '19:30';
if (date('H:i') == date('H:i', strtotime($myTime))) {
    // do something
}

这篇关于PHP:如何将时间字符串与date('H:i')进行比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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