日期比较错误 [英] wrong in date comparison

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

问题描述

$ips = file_get_contents($_SERVER['DOCUMENT_ROOT']."/visitors.txt");
$arr = explode(",",$ips);

$today =  strtotime(date('Y-m-d H:i:s'));

for ($n = 0, $max = count($arr); $n <= $max; $n++) {
 $visArr = explode("#",$arr[$n]);
 $visDate = strtotime($visArr[1]); //$visArr[1] = 2011-12-27 14:10:45
 if($visDate < $today){
    unset ($arr[$n]);   
 }
}
 print_r($arr); //empty array!

像这样存储的数据

 xxx.xxx.xxx.xxx#2011-12-27 11:56:24,

 xxx.xxx.xxx.xxx#2011-12-28 11:56:24,

当我比较访客日期时结果不正确,请问我知道这是怎么回事

the result is not correct when i do compare for visitor date, may i know what's wrong with it?

编辑:我固定了我的代码,比较两个日期时仍然给出了一个空数组!

i fixed up my code and still give an empty array when compare two date!! any advice??

推荐答案

有很多问题,但这是最明显的事情:

There are a lot of things wrong with it, but here are the most obvious things:


  • $ arr ,\r\爆炸n 表示逗号将不再出现在任何结果数组元素中,因为定界符将被丢弃(除非数据中的内容多于您最初发布的内容)。

  • 在第一个 foreach 循环中您实际上并没有使用 $ key ,因此您可能应该忽略它

  • $ arrr 在第一个循环中不断被覆盖,这意味着它将仅包含最后一个元素中逗号分隔的数据。循环完成后的 $ arr 的值。因为上一个调用 explode()会删除您的尾随逗号,所以在这一点上很可能是一无所有。

  • 您在第二个循环条件中使用了 count()函数。计数没有变化,因此您应该将其放在循环本身之前或这样的第一部分中:

  • $arr is being exploded by ,\r\n meaning the commas will no longer be in any of the resulting array elements, because delimiters are discarded (unless there is more to the data than what you originally posted).
  • You aren't actually using $key in the first foreach loop, so you should probably just omit it.
  • $arrr is being constantly overwritten in the first loop, meaning it will only contain the comma-delimited data from the last element of $arr when the loop is done. Because your trailing commas are removed by the previous call to explode(), this is likely to be an array of nothing at this point.
  • You're using the count() function in the second loop condition. The count doesn't change, so you should put this either before the loop itself or in the first part like this:

for($ n = 0,$ max = count($ arrr); $ n< = $ max; $ n ++){

您在第二个循环中使用< = ,因此您进行了不必要的迭代。 n 个元素的数组的最大索引为 n-1 。在这种情况下,应使用< ,因为未定义 $ arrr [count($ arrr)]

You're using <= in the second loop, so you're doing an extra unneeded iteration. The maximum index for an array of n elements is n - 1. You should use < in this case, because $arrr[count($arrr)] is not defined.

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

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