PHP関数strtotime()の使用法は次のとおりです。
int strtotime ( string $time [, int $now ] )
これは、時刻の文字列値を渡し、オプションで現在の時刻の値(UNIXタイムスタンプ)を渡すことを意味します。返される値は、UNIXタイムスタンプである整数です。
この使用例は次のとおりです。strtotime()に渡される日付は、データベースクエリなどからの日付である可能性があります。
$ts = strtotime('2007-12-21');
これにより、値1198148400が$ ts変数に返されます。これは、2007年12月21日の日付のUNIXタイムスタンプです。これは、次のようにdate()関数を使用して確認できます。
echo date('Y-m-d', 1198148400);
// echos 2007-12-21
strtotime()は、実際の日付と、「来週」、「次の火曜日」、「先週の木曜日」、「2週間前」などの文字列を使用して、さまざまな文字列を解析し、適切なタイムスタンプに変換できます。の上。ここではいくつかの例を示します。
$ts = strtotime('21 december 2007');
echo $ts, '<br />';
echo date('Y-m-d', $ts), '<br />';
これにより、次のように表示されます。
1198148400 2007-12-21
今日が12月21日の場合、次のようになります。
$ts = strtotime('next week');
echo $ts, '<br />';
echo date('Y-m-d', $ts), '<br />';
$ts = strtotime('next tuesday');
echo $ts, '<br />';
echo date('Y-m-d', $ts), '<br />';
$ts = strtotime('last thursday');
echo $ts, '<br />';
echo date('Y-m-d', $ts), '<br />';
$ts = strtotime('2 weeks ago');
echo $ts, '<br />';
echo date('Y-m-d', $ts), '<br />';
$ts = strtotime('+ 1 month');
echo $ts, '<br />';
echo date('Y-m-d', $ts), '<br />';
次のように表示されます。
1199006542
2007-12-30
1198494000
2007-12-25
1198062000
2007-12-20
1197192142
2007-12-09
1201080142
2008-01-23