1

echo ($timestamp)与える"2012-05-03 15:35:46"

PHPコマンドは次のようになります。"if $timestamp is older than 12 weeks then…"

助けてくれてありがとう!

4

4 に答える 4

16

これは、マイクロ最適化の観点から、PHPでそれを行うための最速の方法です。

if(strtotime($timestamp) <= time() - (60*60*24*7*12)){
    // 60 secs * 60 mins * 24 hours * 7 days * 12 weeks
}

タイムスタンプをUNIX時間に変換する方法については、 strtotimeについてお読みください。時間関数についても読んでください。

于 2012-05-03T13:50:39.843 に答える
6

必要なのはstrtotime()

$timestamp = strtotime($timestamp);
$twelveWeeksAgo = strtotime('-12 weeks');

if($timestamp < $twelveWeeksAgo) {
 // do something
}

これをチェックする方法は複数ありますが、これは一目で最も説明的なIMHOです。

于 2012-05-03T13:51:49.760 に答える
0
$time_to_compare  = new DateTime();
$twelve_weeks_ago = new DateTime ("-12 weeks");
if ($time_to_compare < $twelve_weeks_ago)
{
    // Do stuff
}
于 2012-05-03T13:51:52.333 に答える
0

php> 5.2を使用している場合は、DateTimeオブジェクトとDateIntervalオブジェクトを使用できます。

$now = new DateTime();
$before = bew DateTime("2012-05-03 15:35:46");

$diff = $now->diff($before);

if ($diff->d > (12 * 7)) {
    print "Older than 12 weeks";
}

上記のオブジェクトのドキュメントはこちらをご覧ください。

于 2012-05-03T13:54:15.563 に答える