次のようにフォーマットされた日付文字列のUNIX時間を取得しようとしています。
'second sunday of march 2010'
'first sunday of november 2010'
strtotimeはそのような文字列を処理できるという印象を受けましたが、これはfalseを返すため、明らかにそうではありません。月の1つ(つまり、1番目、2番目など)、1か月、1年の曜日を指定した場合、UNIX時間に変換するにはどうすればよいですか。
これはで可能になるはずstrtotime
です。3月の初日のタイムスタンプを生成し、mktime()
それを2番目のパラメーターとして追加してみることができます(文字列部分に「最初の日曜日」だけを残します)。
$timestamp = mktime (0,0,0,3,1,2010); // 1st of march
$first_sunday = strtotime("first sunday", $timestamp);
これが実際に日曜日である初日(3月1日)をどのように処理するかはわかりません。必ずチェックしてください。
また、おそらくこれはより信頼性が高いので、これをチェックしてください-ポスターは、次の表記(引用)で良い結果が得られたと述べています:
<?php
strtotime('+0 week sun nov 2009'); // first sunday in nov 2009
strtotime('+1 week sun nov 2009'); // second sunday
strtotime('-1 week sun nov 2009'); // last sunday in oct 2009
?>
strtotimeの場合と同様に、選択するものが何であれ、特にエッジケースをよくテストするようにしてください(月の最初の日は日曜日、先月の最後の日は日曜日でした...)
あなたのコードはPHP5.3.0で動作します。どのバージョンのPHPを使用していますか?
<?php
date_default_timezone_set("Europe/Oslo");
$time_march = strtotime('second sunday of march 2010');
$time_november = strtotime('first sunday of november 2010');
echo date("Y-m-d", $time_march) . " (timestamp: $time_march)\n";
echo date("Y-m-d", $time_november) . " (timestamp: $time_november)\n";
?>
与える:
2010-03-14 (timestamp: 1268521200)
2010-11-07 (timestamp: 1289084400)