PHP之间的日期 [英] PHP Date Between

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

问题描述

需要检查一下给定日期在两个给定日期之间使用PHP,这是我写的代码:

Need to check wether a given date is in between two given dates using PHP, This is the code I've written:

$today = date_create('28-Feb-2012');
$fromdate = date_create('20-Feb-2012');
$todate = date_create('22-Feb-2012'); 
if ($today>=$fromdate && $todat<=$todate)
{
    echo 'in range';
}
else
{
    echo 'not in range';
}

但这总是返回范围 $ c>。无法弄明白问题所在。请帮助。

But this always returns in range. Can't figure out where the problem is. Please help.

推荐答案

我知道这有点麻烦的解决方案,但我倾向于使用strtotime()事情,因为它返回一个整数:

I know it's a bit of a hacky solution, but I tend to use strtotime() for that kind of thing, as it returns an integer:

$today = time();
$fromdate = strtotime('20-Feb-2012');
$todate = strtotime('22-Feb-2012'); 
if ($today>=$fromdate && $today<=$todate)
{
    echo 'in range';
}
else
{
    echo 'not in range';
}

编辑:我假设 $ todat 只是一个打字到stackoverflow,而在你的代码中你实际上有$ code $ $ $ / code>,但如果不是这个可能是你的问题 - 未定义的变量,在某些设置,可以考虑为0我相信。

i assume the $todat was just a typo into stackoverflow, and that in your code you actually have $today, but if not that might be your problem - undefined variables, in some settings, can be considered 0 I believe.

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

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