2012-09-23 79 views
2

因此可以将时间与<,>等进行比较,但与strtotime一起使用时,会得到unix时间戳,因此您无法比较与日期无关的时间。比较不带日期的日期

我希望做的是能够表达这样的:

$reference_time = "2010-01-01 18:00:00"; 
print strtotime("19:00:00") > strtotime($reference_time); 
print strtotime("16:00:00") > strtotime($reference_time); 

但我想第二个表达式返回false(因为忽略了日期,时间短)。什么是最简单的方法来做到这一点? (最好不要拆分字符串)

+0

这已经被问过,请使用搜索功能。 – hakre

+0

@hakra:我确实看过,但无法找到任何东西。这似乎是一个共同的需要... – amindfv

+0

问题是(重复)日期/时间问题的数量是很大,你甚至无法找到现在快速正确的东西。但是我也发现发布答案比搜索更容易:/ – hakre

回答

3

strtotime也有第二个参数。尝试:

$reference_time = "2010-01-01 18:00:00"; 
$absolute = strtotime($reference_time); 

print strtotime("19:00:00", $absolute) > $absolute; 
print strtotime("16:00:00", $absolute) > $absolute; 

演示:http://codepad.org/uG4bsxTH

+0

美丽,谢谢! – amindfv

1

您可以将每个Unix时间戳的模数相对于86400(一天中的秒数)。

http://php.net/manual/en/language.operators.arithmetic.php

+1

这不是用UNIX时间戳工作的方式。您只能计算秒数,但一天中的秒数*会变化*。 – hakre

+0

还链接PHP的操作码页面?为什么这应该是有用的? – hakre

+0

你是绝对正确的,快乐。我的回答有点聪明,而且错了。 – jrajav

0

你可以这样做:

print strtotime("19:00:00") > strtotime(explode(' ',$reference_time)[1]); 
print strtotime("16:00:00") > strtotime(explode(' ',$reference_time)[1]); 

你需要PHP 5.4作为对的最小值上面的代码工作。