Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/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 如果文章在12个月后发布,请停止检索文章_Php_Date_While Loop_Comparison - Fatal编程技术网

Php 如果文章在12个月后发布,请停止检索文章

Php 如果文章在12个月后发布,请停止检索文章,php,date,while-loop,comparison,Php,Date,While Loop,Comparison,我正在抓取一页文章,其中包含以下格式的日期: 2012-08-20T11:04:00+0200 我想做的是,如果下一篇文章从今天起12个月后发布,则停止检索文章。我能想到的方法如下: while ($retrieveArticles == true) { $stopDate = date('Y-m-d'); // <--- this gives me todays date while I need the date 12 months ago. $date = $a

我正在抓取一页文章,其中包含以下格式的日期:

2012-08-20T11:04:00+0200
我想做的是,如果下一篇文章从今天起12个月后发布,则停止检索文章。我能想到的方法如下:

while ($retrieveArticles == true) {

    $stopDate = date('Y-m-d'); // <--- this gives me todays date while I need the date 12 months ago.

    $date = $article->find('header div p span', 1);
    $date = substr($date->title, 0, 10); // <--- becomes 2012-08-20

    if ($date >= $stopDate) {
        $retrieveArticles = false;
    }

    ... not relevant code

}
while($retrieveArticles==true){
$stopDate=date('Y-m-d');//find('header div p span',1);
$date=substr($date->title,0,10);//=$stopDate){
$retrieveArticles=false;
}
…不相关代码
}
我需要帮助的是:
  • 我如何从今天的日期减去12个月

  • 我这样做是对的,还是有更好更优雅的方式来实现我想要的


  • 提前谢谢

    2012-08-20T11:04:00+0200
    转换为时间戳:

    然后只需执行
    $seconds=time()-$theresult
    这将是此后的秒数。12个月大约等于3100万秒

    2012-08-20T11:04:00+0200
    转换为时间戳:
    然后只需执行
    $seconds=time()-$theresult
    这将是此后的秒数。12个月大概相当于3100万秒

    是的,可以肯定:

    $in_12_months = strtotime('+12 months');
    
    while ($retrieveArticles == true) {
      $article_date = strtotime($article->find('header div p span', 1));
    
      if ($article_date >= $in_12_months) {
        $retrieveArticles = false;
      }
    }
    
    当然可以:

    $in_12_months = strtotime('+12 months');
    
    while ($retrieveArticles == true) {
      $article_date = strtotime($article->find('header div p span', 1));
    
      if ($article_date >= $in_12_months) {
        $retrieveArticles = false;
      }
    }
    
    我是这样做的:

    <?php
    $s = strtotime('2012-02-09T11:04:00+0200');
    $timeDifference = time() - $s;
    echo round($timeDifference / 60 / 60 / 24 / 30);
    ?>
    
    
    
    输出:
    11

    我是这样做的:

    <?php
    $s = strtotime('2012-02-09T11:04:00+0200');
    $timeDifference = time() - $s;
    echo round($timeDifference / 60 / 60 / 24 / 30);
    ?>
    
    
    
    输出:
    11

    您可以这样做:

    <?php
    // Current date
    $posted = strtotime("2012-08-20T11:04:00+0200");
    
    // 12 months ago
    $timestamp = strtotime("-12 months", $posted);
    
    // days
    $days = ($posted - $timestamp) / 60 / 60 / 24;
    
    $get_items = true;
    if($days >= 365){
        $get_items = false;
    }
    

    您可以这样做:

    <?php
    // Current date
    $posted = strtotime("2012-08-20T11:04:00+0200");
    
    // 12 months ago
    $timestamp = strtotime("-12 months", $posted);
    
    // days
    $days = ($posted - $timestamp) / 60 / 60 / 24;
    
    $get_items = true;
    if($days >= 365){
        $get_items = false;
    }
    

    如果将日期的Y-m-d格式与一起比较,则会出现错误:
    您需要使用strottime()函数将其转换为时间格式。 12个月,即365*24*3600秒。因此,您可以这样更改函数:

    while ($retrieveArticles == true) {
    
        $stopDate = date('Y-m-d'); // <--- this gives me todays date while I need the date 12 months ago.
    
        $date = $article->find('header div p span', 1);
        $date = substr($date->title, 0, 10); // <--- becomes 2012-08-20
    
        $stopDate = strtotime($stopDate);
        $date = (int)strtotime($date)  + (365*24*3600);
        if ($stopDate >= $date) {
            $retrieveArticles = false;
        }
    }
    
    while($retrieveArticles==true){
    $stopDate=date('Y-m-d');//find('header div p span',1);
    $date=substr($date->title,0,10);//=$date){
    $retrieveArticles=false;
    }
    }
    
    如果将日期的Y-m-d格式与一起比较,则会出现错误:
    您需要使用strottime()函数将其转换为时间格式。 12个月,即365*24*3600秒。因此,您可以这样更改函数:

    while ($retrieveArticles == true) {
    
        $stopDate = date('Y-m-d'); // <--- this gives me todays date while I need the date 12 months ago.
    
        $date = $article->find('header div p span', 1);
        $date = substr($date->title, 0, 10); // <--- becomes 2012-08-20
    
        $stopDate = strtotime($stopDate);
        $date = (int)strtotime($date)  + (365*24*3600);
        if ($stopDate >= $date) {
            $retrieveArticles = false;
        }
    }
    
    while($retrieveArticles==true){
    $stopDate=date('Y-m-d');//find('header div p span',1);
    $date=substr($date->title,0,10);//=$date){
    $retrieveArticles=false;
    }
    }
    
    也许
    strotime()
    能帮你吗?它会将该时间转换为UNIX时间戳。
    $when=strotime('12个月前')也许
    strotime()
    能帮你吗?它会将该时间转换为UNIX时间戳。
    $when=strotime('12个月前')为什么在“$date=(int)strottime($date)”之后添加“+(365*24*3600);”?对我来说似乎是错的?我在“发布日期”上加了12个月,然后检查它是否比现在短,这样就不会显示帖子了。啊,你这样做,是的,应该行。将返回:)为什么在“$date=(int)strottime($date)”之后添加“+(365*24*3600);”?对我来说似乎是错的?我在“发布日期”上加了12个月,然后检查它是否比现在短,这样就不会显示帖子了。啊,你这样做,是的,应该行。将返回:)