Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将当前日期与从文本文件写入的日期进行比较少于一周_Php_File_Datetime_File Io - Fatal编程技术网

Php 将当前日期与从文本文件写入的日期进行比较少于一周

Php 将当前日期与从文本文件写入的日期进行比较少于一周,php,file,datetime,file-io,Php,File,Datetime,File Io,我逐行阅读文本文件,每行的格式如下 05/20/2014 10:18:28 am m/d/y h:m:s 如何将其与当前日期进行比较,并选择不少于一周的日期? 这就是我迄今为止所做的,但我想知道是否还有其他更简单的方法: $arr = explode(' ',trim($line)); $fword = explode("/", $fword); $fword = $arr[0]; list($m,$d,$y) = explode("/", $line); mm= date('m'); d

我逐行阅读文本文件,每行的格式如下

05/20/2014 10:18:28 am

m/d/y h:m:s
如何将其与当前日期进行比较,并选择不少于一周的日期? 这就是我迄今为止所做的,但我想知道是否还有其他更简单的方法:

$arr = explode(' ',trim($line));
$fword = explode("/", $fword);
$fword =  $arr[0];
list($m,$d,$y) = explode("/", $line);
mm= date('m');
dd = date('d');
顺便说一下,我正在以以下格式写入文本文件:

$date = date('m/d/Y h:i:s a', time());

您可以使用
strotime

$current_time = strtotime(date('d-M-Y g:i:s A'));

$one_week = 3600 * 24 * 7;

// your file code
$fword =  $arr[0]; // assuming that $fword has file date.

// $fword = str_replace("/", "-", $fword);
$file_time = strtotime($fword);

$diff = $current_time - $file_time;
if($diff>$one_week)
{
   echo " date : ".$fowrd." is older than one week <br/>";
}
else 
{
   echo " date : ".$fowrd." is not older than one week <br/>";  
}
$current_time=strottime(日期('d-M-Y g:i:sa');
每周一美元=3600*24*7;
//您的文件代码
$fword=$arr[0];//假设$fword具有文件日期。
//$fword=str_替换(“/”、“-”、$fword);
$file_time=strottime($fword);
$diff=$current\u time-$file\u time;
如果($diff>一周一美元)
{
echo“日期:.$fowrd.”早于一周
; } 其他的 { echo“日期:.$fowrd.”不超过一周
; }
只需使用date和strotime方法

$t = '05/02/2014 10:18:28 am';
$aWeekLater = date('Y-m-d H:i:s', strtotime($t.' -1 week'));
$curDate = date('Y-m-d H:i:s');

if ($curDate < $aWeekLater) {
    echo 'xxx';
} else {
    echo 'yyy';
}
$t='2014年2月5日上午10:18:28';
$aWeekLater=日期('Y-m-d H:i:s',标准时间('t.-1周');
$curDate=日期('Y-m-d H:i:s');
如果($curDate<$aWeekLater){
回声“xxx”;
}否则{
回音'yyy';
}
if(60*60*24*7+time()
忘记字符串操作和日期函数,改用DateTime类-它是为此类任务设计的

//Current time
$now = new DateTimeImmutable();
//One week ago
$oneWeekAgo = $now->sub(new DateInterval('P1W'));

//Read whole file into array
$lines = file('test.txt', \FILE_SKIP_EMPTY_LINES | \FILE_IGNORE_NEW_LINES);

foreach ($lines AS $line) {
    $date = DateTime::createFromFormat('m/d/Y h:i:s a+', $line);
    //Here you can compare your dates like any other variables
    if ($date > $oneWeekAgo) {
        //Current date is less than 1 week "old"
    }
    if ($date < $oneWeekAgo) {
        //Current date is more than 1 week "old"
    }
}
//当前时间
$now=new DateTimeImmutable();
//一周前
$oneWeekAgo=$now->sub(新日期间隔('P1W');
//将整个文件读入数组
$lines=file('test.txt',\file\u SKIP\u EMPTY\u lines\file\u IGNORE\u NEW\u lines);
foreach($line作为$line){
$date=DateTime::createFromFormat('m/d/Y h:i:SA+',$line);
//在这里,您可以像其他变量一样比较日期
如果($date>$oneWeekAgo){
//当前日期少于1周“旧”
}
如果($date<$oneWeekAgo){
//当前日期已超过1周“旧”
}
}

有关不同的日期/时间段,请参见。例如,如果你想为“1天前”做同样的事情,它将是->(新DateInterval(‘P1D’)),“未来1年”将是->加法(新的DateInterval(‘P1Y’))。如果你需要考虑一天中的DST变化,这将是正确的方法。我想找到一个不到一个星期的$T,但我总是得到YYY?我做错什么了吗?$line=04/20/2014 10:18:28 Am但我得到了yyy如果你得到yyy,这意味着curDate大于$t-1周(显然,5月20日大于4月25日)。谢谢你的回答。我复制粘贴,但对于此行==>2014年5月20日上午10:18:28===>当前日期超过1周old@Bernard是的,有一个打字错误,它现在应该可以工作了-见编辑后的答案。仍然不能工作。即使是今天的日期,它也会显示超过1周的时间。@Bernard那么,从您的文件中获取的日期格式似乎有问题。我在本地机器上进行了测试,对于“m/d/Y h:I:s a”格式,它工作得非常好。您可以尝试使用var_dump($line);和var_dump($日期);在foreach循环中查看发生了什么。如果var_转储($日期);如果为false,则日期格式有问题-请参阅以获取格式。@Bernard您也可以使用'm/d/Y h:i:s a+'格式(注意额外+标记),我已将其包含在更新的答案中。我刚刚检查了今天的日期,即$fword=05/20/2014==>但我得到的日期早于一周。没有问题删除行(
$fword=str\u replace(“/”,“-”,$fword);或尝试更新代码。@Bernard您找到解决方案了吗。
//Current time
$now = new DateTimeImmutable();
//One week ago
$oneWeekAgo = $now->sub(new DateInterval('P1W'));

//Read whole file into array
$lines = file('test.txt', \FILE_SKIP_EMPTY_LINES | \FILE_IGNORE_NEW_LINES);

foreach ($lines AS $line) {
    $date = DateTime::createFromFormat('m/d/Y h:i:s a+', $line);
    //Here you can compare your dates like any other variables
    if ($date > $oneWeekAgo) {
        //Current date is less than 1 week "old"
    }
    if ($date < $oneWeekAgo) {
        //Current date is more than 1 week "old"
    }
}