如何检查当前日期/时间是否超过了设定的日期/时间? [英] How can I check if the current date/time is past a set date/time?

查看:33
本文介绍了如何检查当前日期/时间是否超过了设定的日期/时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个脚本来检查当前日期/时间是否超过 05/15/2010 下午 4 点

I'm trying to write a script that will check if the current date/time is past the 05/15/2010 at 4PM

如何使用 PHP 的 date() 函数来执行此检查?

How can I use PHP's date() function to perform this check?

推荐答案

自 PHP >= 5.2.2 起,您可以使用 DateTime 类:

Since PHP >= 5.2.2 you can use the DateTime class as such:

if (new DateTime() > new DateTime("2010-05-15 16:00:00")) {
    # current time is greater than 2010-05-15 16:00:00
    # in other words, 2010-05-15 16:00:00 has passed
}

传递给DateTime构造函数的字符串被解析根据这些规则.

The string passed to the DateTime constructor is parsed according to these rules.

请注意,也可以使用 timestrtotime 函数.查看原始答案.

Note that it is also possible to use time and strtotime functions. See original answer.

这篇关于如何检查当前日期/时间是否超过了设定的日期/时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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