0

次の形式の日付をすべて含む記事のページをスクレイピングしています。

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

}

助けが必要なこと:

  1. 今日の日付から 12 か月を減算するにはどうすればよいですか?

  2. このようにすることで私は正しいと考えていますか、それとも私が望むものを達成するためのより良い、よりエレガントな方法はありますか?

前もって感謝します!

4

5 に答える 5

1

これが私がそれをした方法です:

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

出力: 11

于 2013-01-17T17:22:22.377 に答える
1

日付の Ymd 形式を一緒に比較すると間違っています
。それを strtotime() 関数で時刻形式に変換する必要があります。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;
    }
}
于 2013-01-17T17:30:03.363 に答える
1

はい、確かに:

$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;
  }
}
于 2013-01-17T17:17:04.543 に答える
0

次のようなことができます。

<?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;
}
于 2013-01-17T17:24:58.007 に答える
0

2012-08-20T11:04:00+0200タイムスタンプに変換: PHPで日付をタイムスタンプに変換する方法は?
そして、$seconds = time()-$theresultこれを行うだけで、それからの秒数になります。12 か月は、およそ 3,100 万秒に等しいはずです

于 2013-01-17T17:16:30.287 に答える