PHP の strtotime 関数 - 文字列から日時を解析する

日時を扱うとき、「2025-07-01」や「next Monday」のような文字列からタイムスタンプを得たい場面は頻繁にあります。strtotime 関数は英語の日時表現を解析し、対応する Unix タイムスタンプを返す関数です。

基本的な使い方

strtotime のシグネチャは次のとおりです。

strtotime(string $datetime, ?int $baseTimestamp = null): int|false

第 1 引数に日時を表す文字列を渡すと、解析結果のタイムスタンプが返ります。解析に失敗した場合は false が返ります。

$ts = strtotime('2025-07-01');
echo date('Y-m-d', $ts);
// 2025-07-01

$ts = strtotime('July 1, 2025');
echo date('Y-m-d', $ts);
// 2025-07-01

ISO 8601 形式の「2025-07-01」でも、英語の月名を使った「July 1, 2025」でも同じタイムスタンプが得られます。strtotime は内部で多様な日時フォーマットを認識するため、入力形式をそこまで厳密に揃えなくても動作します。

相対的な日時表現

strtotime の真価が発揮されるのは、相対的な日時表現を解析できる点です。「今日から 3 日後」「来週の月曜日」といった表現を英語で書くだけでタイムスタンプに変換されます。

echo date('Y-m-d', strtotime('+3 days'));
// 今日から3日後

echo date('Y-m-d', strtotime('-1 week'));
// 1週間前

echo date('Y-m-d', strtotime('+2 months'));
// 2か月後

echo date('Y-m-d H:i:s', strtotime('+1 hour 30 minutes'));
// 1時間30分後

加算には +、減算には - を前置します。単位は day・week・month・year・hour・minute・second などが使え、複数の単位を組み合わせることも可能です。

名前付きの相対表現

数値を使わない名前付きの表現も豊富に用意されています。

表現意味備考
now現在time() と同等
today今日の 0:00:00時刻部分がリセット
tomorrow明日の 0:00:00today + 1 day
yesterday昨日の 0:00:00today - 1 day
echo date('Y-m-d H:i:s', strtotime('today'));
// 例: 2025-06-15 00:00:00

echo date('Y-m-d H:i:s', strtotime('tomorrow'));
// 例: 2025-06-16 00:00:00

today を使うと時刻部分が 0 時 0 分 0 秒にリセットされるため、「今日の開始時点」を基準にしたい場合に便利です。

曜日を指定する表現

特定の曜日を指す表現も解析できます。

echo date('Y-m-d', strtotime('next Monday'));
// 次の月曜日

echo date('Y-m-d', strtotime('last Friday'));
// 前の金曜日

echo date('Y-m-d', strtotime('this Wednesday'));
// 今週の水曜日

next・last・this と曜日名を組み合わせるだけで、カレンダー計算を自前で行う手間が省けます。

月の初日・末日を求める

月の初日や末日も文字列で表現できます。

月初の取得

strtotime(‘first day of this month’) で今月 1 日のタイムスタンプが返る。

月末の取得

strtotime(‘last day of this month’) で今月末日のタイムスタンプが返る。

echo date('Y-m-d', strtotime('first day of this month'));
// 例: 2025-06-01

echo date('Y-m-d', strtotime('last day of this month'));
// 例: 2025-06-30

echo date('Y-m-d', strtotime('first day of next month'));
// 例: 2025-07-01

mktime で翌月の 0 日を指定するテクニックと比べると、strtotime のほうが意図が読み取りやすいコードになります。

第 2 引数で基準時刻を変える

第 2 引数に基準となるタイムスタンプを渡すと、相対表現の起点がその時刻に変わります。

$base = strtotime('2025-01-15');

echo date('Y-m-d', strtotime('+10 days', $base));
// 2025-01-25

echo date('Y-m-d', strtotime('next Monday', $base));
// 2025-01-20

第 2 引数を省略した場合は現在時刻が基準になります。過去や未来の特定の日付を起点にして相対計算を行いたいときに、この引数が役立ちます。

注意点:月の加減算と日の繰り上がり

strtotime で月を加減算する際、元の日が存在しない月にはみ出すと日が繰り上がります。

// 1月31日の1か月後
echo date('Y-m-d', strtotime('+1 month', strtotime('2025-01-31')));
// 2025-03-03

1 月 31 日に 1 か月を足すと「2 月 31 日」となりますが、2 月は 28 日(または 29 日)までしかないため、余った日数が 3 月に繰り上がって 3 月 3 日になります。この挙動は意図しない結果を生みやすいので、月末の計算には last day of を使ったほうが安全です。

echo date('Y-m-d', strtotime('last day of +1 month', strtotime('2025-01-31')));
// 2025-02-28

解析失敗への対処

strtotime は解析に失敗すると false を返します。ユーザー入力など信頼できない文字列を渡す場合は、戻り値のチェックが不可欠です。

$input = 'not a date';
$ts = strtotime($input);

if ($ts === false) {
    echo '日時の解析に失敗しました';
}

strtotime は英語の日時表現を対象としているため、「2025年7月1日」のような日本語の文字列は解析できません。日本語の日時文字列を扱う場合は、正規表現で分解してから mktime に渡すか、後述の DateTime クラスの createFromFormat メソッドを利用する方法があります。