douquanqiao6788 2013-08-13 15:09 采纳率: 100%
浏览 28
已采纳

日期/时间变量不尊重if语句

I am running a program in which part of it iterates through a directory and stores the most recent date in a variable and the name of the most recent folder in another. I have a series of alerts set up so that i can debug.

Alerts Order:

  • Expresses current sub-directory and time
  • Then the variable with the subdirectory date dubbed "temp" and latest date .
  • Then "update time" if temp is more recent then the "latest"
  • Then "Time changed" if temp is more recent then the "latest"
  • Then it expresses the new latest time and latest folder name associated with that date

My problem is through the first subdirectory this all behaves normal. Then the second directory it gets messed up. The first 2 alerts are expected (the second directory is older than the first by the way). The third and fourth alerts are skipped (as expected). but the 5th alert shows that the latest folder variable has been changed to the current subdirectory (yet the latest time remains with the past iterated subdirectory time).

hope this makes sense....here is the code

<?php  
    $files = array();
    $latestTime = date("1900-01-01"); ///older then any of the folders will be
    $latestFolder = "none";
    foreach (new DirectoryIterator('./images/ISGC_images/') as $fileInfo) { ///iterate through directory
        if($fileInfo =="."|$fileInfo == "..") continue;
        if($fileInfo->isDir()) { 
                echo "<script type='text/javascript'>alert('".$fileInfo." was updated ".date("F d Y H:i:s.",filemtime('./images/ISGC_images/'.$fileInfo))."');</script>";
                $tempDate = date("F d Y H:i:s.",filemtime('./images/ISGC_images/'.$fileInfo));
                echo "<script type='text/javascript'>alert('"."temp time is ".$tempDate.'and latest time is'.$latestTime."');</script>";
                if ($tempDate > $latestTime)
                    echo "<script type='text/javascript'>alert('"."update time"."');</script>"; 
                if ($tempDate > $latestTime) { 
                    $latestFolder = $fileInfo;
                    $latestTime = $tempDate;
                    echo "<script type='text/javascript'>alert('"."Time Changed!"."');</script>"; 
                }
                echo "<script type='text/javascript'>alert('"."latest folder is ".$latestFolder."');</script>"; 
                echo "<script type='text/javascript'>alert('"."latest time is ".$latestTime."');</script>"; 
            }                    
        }              

?>

ANSWER

Line 14 needs to be changed to $latestFolder = (string)$fileInfo; because a folder object cannot be stored in a variable apparently

  • 写回答

1条回答 默认 最新

  • douliao5467 2013-08-13 15:22
    关注

    You're directly comparing your dates while they're still strings. This seems to be defaulting to alphabetical string compare, so you're saying "01-01-1980" > "01-01-1900", which may not hold for the next string comparison, just as "apple" > "pear".

    Try it with the actual time comparison instead. e.g.:

     if (strtotime($tempDate) > strtotime($latestTime)
    

    Also this part doesn't seem to make sense:

    if($fileInfo->isDir()) { 
        (...)
        if (file_exists('./images/ISGC_images/'.$fileInfo));
    

    So even if it's a directory you do file_exists('folder'.)? Of course this would not be a file. To loop over the files in the subdirectory you would have to do something similar to what you did on top.

    Try to work it out yourself with these hints.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误