简体   繁体   English

PHP时间戳比较

[英]PHP timestamp comparison

$today      = mktime(0,0,0,2, 9, 2011);
$today >= $r['arrival_date'] // false
9 >= date('j', $r['arrival_date']) // true

$r['arival_date'] is 1297227600 [Feb 9, 2011 07:00]

Simple: 简单:

$today = mktime(0,0,0,2, 9, 2011); // = 1297209600

and

$r['arival_date'] = 1297227600;

so 所以

1297227600 > 1297209600

because 因为

date('H',$r['arrival_date']); // 7
date('H',$today); // 0

To expand and explain upon Andre Matos' answer, mktime(0,0,0,2,9,2011); 扩展并解释Andre Matos的答案, mktime(0,0,0,2,9,2011); is 00:00:00 Feb 9 2011 , basically the first instant of Feb 9, and the Arrival Date is 07:00:00 Feb 9 2011 , 7 hours later, so it's timestamp is greater than the one created by mktime. 00:00:00 Feb 9 2011 ,基本上是00:00:00 Feb 9 2011的第一时刻,到达日期是07:00:00 Feb 9 2011 ,即7小时后,因此它的时间戳大于mktime创建的时间戳。

To check whether or not a timestamp falls within a specific day, you can check in a few different ways: 要检查时间戳记是否在特定的一天之内,您可以通过几种不同的方式进行检查:

//You can check by adding a day onto the timestamp for today, 24*60*60 is one days worth of seconds (86400 seconds)
if($r['arrival_date'] >= $today && $r['arrival_date'] <= $today + (24*60*60))

//Or you can mktime for tomorrow too.  
$tomorrow = mktime(0,0,0,2,10,2011);
if($r['arrival_date'] >= $today && $r['arrival_date'] <= $tomorrow)

//Or you could check the way you have up there, by running it through date and checking if one is equivalent to another
//Or you could do strtotime in there somewhere, or whatever

That's just a couple of the easiest. 这只是最简单的几个。 Basically since timestamps are down to the second (specifically seconds since 00:00:00 Jan 1 1970 UTC ), you're gonna have to check them by range. 基本上,因为时间戳减少到了第二个(特别是自00:00:00 Jan 1 1970 UTC以来的秒),所以您必须按范围检查它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM