24
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PHPで日、月、年の加算と減算方法

Last updated at Posted at 2020-10-16

PHPでは、strtotime()関数やDateTimeクラスを使って、
「明日」「1週間後」「1時間後」などの日付加算や、
「昨日」「1年前」などの日付減算が簡単にできます。

✅ 注意点:月末の処理について
月末から「1ヶ月前」や「1ヶ月後」を取得する場合、日付のずれに注意が必要です。
たとえば「2023-03-31」に1ヶ月を加算すると、「2023-04-31」は存在しないため、自動で「2023-05-01」になるなどの調整が入ります。

正確に月末日を取得したい場合は last day of previous month を使うのがおすすめです。

strtotime関数を使う

strtotime関数の詳細はこちらを参考にしてください。
YYYY-mm-ddは元となる日付です。

加算例

echo date("Y-m-d", strtotime("+1 day"));   // 明日
echo date("Y-m-d", strtotime("+1 week"));  // 1週間後
echo date("Y-m-d", strtotime("+1 month")); // 1ヶ月後
echo date("Y-m-d", strtotime("+1 year"));  // 1年後

//例
echo date("Y-m-d", strtotime("1 day")); // 2023-01-31

減算例

echo date("Y-m-d", strtotime("-1 day"));   // 昨日
echo date("Y-m-d", strtotime("-1 week"));  // 1週間前
echo date("Y-m-d", strtotime("-1 month")); // 1ヶ月前
echo date("Y-m-d", strtotime("-1 year"));  // 1年前

//例
echo date("Y-m-d", strtotime("-1 day")); // 2022-12-30

DateTimeクラスを使う

DateTimeクラスの詳細はこちらを参考にしてください。
YYYY-mm-ddは元となる日付です。

加算の例

$date = new DateTime('now'); // 現在日時を基準に
$date->modify('+1 day');
$date->modify('+1 week');
$date->modify('+1 month');
$date->modify('+1 year');

$date->modify('+1 day +1 week +1 month +1 year'); // 複数の加算も可能

// 表示フォーマットは自由に変更できます。
echo $date->format('Y-m-d');

//例
$date = new DateTime('2022-12-31');
$date->modify('+1 day');
echo $date->format('Y-m-d'); // 2023-01-01

減算の例

$date = new DateTime('now');
$date->modify('-1 day');
$date->modify('-1 week');
$date->modify('-1 month');
$date->modify('-1 year');

$date->modify('-1 day -1 week -1 month -1 year'); // 複数の減算も可能

// 表示フォーマットは自由に変更できます。
echo $date->format('Y-m-d');

//例
$date = new DateTime('2022-12-31');
$date->modify('-1 day');
echo $date->format('Y-m-d'); // 2022-12-30

日数算出

2つの日付の日数差を計算(日数のみ)

$date1 = strtotime("2023-02-03");
$date2 = strtotime("2023-02-02");

// 秒数を86400で割ると日数になります
// 86400 = (60 * 60 * 24)
$diff = ($date1 - $date2) / 86400; // 秒数を日数に変換
echo $diff . '日'; // => 1日

※UNIXタイムスタンプは、1970年1月1日00時00分00秒UTC(協定世界時と一致する標準時)らの経過秒数です。例えば2019年5月15日1時34分25秒のUNIXタイムスタンプは 1557851665 となります。

フォーマット一覧

フォーマット 説明
now 現在の日時
tomorrow 明日
yesterday 昨日
+1 day 1日後
+1 week 1週間後
+1 month 1ヶ月後
+1 year 1年後
+1 hour 1時間後
+1 minute 1分後
+1 second 1秒後
last day of previous month 先月の末日
項目 フォーマット 説明 サンプル
Y 西暦(4桁) 2024
y 西暦(2桁) 24
L うるう年→1、普通の年→0 0, 1
m 月(2桁) 01〜12
n 月(先頭の0なし) 1〜12
M 英語(略語) Jan〜Dec
F 英語 January〜December
d 日(2桁) 01〜31
j 日(先頭の0なし) 1〜31
t その月の日数 28〜31
z その年の経過日数 0〜365
曜日・週 D 英語(略語) Mon〜Sun
l 英語 Sunday〜Saturday
w 曜日(日曜0→土6) 2
W ISO 8601 月曜日に
始まる年単位の週番号
42 (年の第 42 週目)
時間 H 24時間単位 00〜23
G 24時間単位(先頭の0なし) 0〜23
h 12時間単位 01〜12
g 12時間単位(先頭の0なし) 1〜12
a 午前/午後(小文字) am
A 午前/午後(大文字) AM
i 分(2桁) 00〜59
s 秒(2桁) 00〜59

その他の記事

【令和も対応】PHPで和暦⇔西暦変換
https://qiita.com/thiagomatsui/items/57201fea2b4853654bec

参考サイト

https://blog.codecamp.jp/php-datetime-calc
https://www.flatflag.nir87.com/modify-495

24
23
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?