今日の人気記事

  1. PHP関数リファレンス

日時を取得 - 明日、昨日、来週、先週、来月、先月、来年、去年 - date() strtotime()

当ページのリンクには広告が含まれています。

ここでは関数 date() と strtotime() を使って、さまざまな日時を取得するサンプルコードを紹介します。

秒(sec)

<?php
// 秒 sec
echo date("Y-m-d H:i:s") ." 現在\n";
echo date("Y-m-d H:i:s", strtotime("-3 sec")) ." 3秒前\n";
echo date("Y-m-d H:i:s", strtotime("-2 sec")) ." 2秒前\n";
echo date("Y-m-d H:i:s", strtotime("-1 sec")) ." 1秒前\n";
echo date("Y-m-d H:i:s", strtotime("1 sec")) ." 1秒後\n";
echo date("Y-m-d H:i:s", strtotime("2 sec")) ." 2秒後\n";
echo date("Y-m-d H:i:s", strtotime("3 sec")) ." 3秒後\n\n";
?>

実行結果です。

2014-09-15 17:35:17 現在
2014-09-15 17:35:14 3秒前
2014-09-15 17:35:15 2秒前
2014-09-15 17:35:16 1秒前
2014-09-15 17:35:18 1秒後
2014-09-15 17:35:19 2秒後
2014-09-15 17:35:20 3秒後

分(min)

<?php
// 分 min
echo date("Y-m-d H:i:s") ." 現在\n";
echo date("Y-m-d H:i:s", strtotime("-3 min")) ." 3分前\n";
echo date("Y-m-d H:i:s", strtotime("-2 min")) ." 2分前\n";
echo date("Y-m-d H:i:s", strtotime("-1 min")) ." 1分前\n";
echo date("Y-m-d H:i:s", strtotime("1 min")) ." 1分後\n";
echo date("Y-m-d H:i:s", strtotime("2 min")) ." 2分後\n";
echo date("Y-m-d H:i:s", strtotime("3 min")) ." 3分後\n\n";
?>

実行結果です。

2014-09-15 17:35:17 現在
2014-09-15 17:32:17 3分前
2014-09-15 17:33:17 2分前
2014-09-15 17:34:17 1分前
2014-09-15 17:36:17 1分後
2014-09-15 17:37:17 2分後
2014-09-15 17:38:17 3分後

時(hour) - 正午

<?php
// 時 hour
echo date("Y-m-d H:i:s") ." 現在\n";
echo date("Y-m-d H:i:s", strtotime("noon")) ." 正午\n";
echo date("Y-m-d H:i:s", strtotime("-3 hour")) ." 3時間前\n";
echo date("Y-m-d H:i:s", strtotime("-2 hour")) ." 2時間前\n";
echo date("Y-m-d H:i:s", strtotime("-1 hour")) ." 1時間前\n";
echo date("Y-m-d H:i:s", strtotime("1 hour")) ." 1時間後\n";
echo date("Y-m-d H:i:s", strtotime("2 hour")) ." 2時間後\n";
echo date("Y-m-d H:i:s", strtotime("3 hour")) ." 3時間後\n\n";
?>

実行結果です。

2014-09-15 17:35:17 現在
2014-09-15 12:00:00 正午
2014-09-15 14:35:17 3時間前
2014-09-15 15:35:17 2時間前
2014-09-15 16:35:17 1時間前
2014-09-15 18:35:17 1時間後
2014-09-15 19:35:17 2時間後
2014-09-15 20:35:17 3時間後

日(day) - 昨日、今日、明日

<?php
// 日 day
echo date("Y-m-d H:i:s", strtotime("yesterday")) ." 昨日\n";
echo date("Y-m-d H:i:s", strtotime("today")) ." 今日\n";
echo date("Y-m-d H:i:s", strtotime("tomorrow")) ." 明日\n";
echo date("Y-m-d H:i:s", strtotime("-3 day")) ." 3日前\n";
echo date("Y-m-d H:i:s", strtotime("-2 day")) ." 2日前\n";
echo date("Y-m-d H:i:s", strtotime("-1 day")) ." 1日前\n";
echo date("Y-m-d H:i:s", strtotime("+1 day")) ." 1日後\n";
echo date("Y-m-d H:i:s", strtotime("+2 day")) ." 2日後\n";
echo date("Y-m-d H:i:s", strtotime("+3 day")) ." 3日後\n\n";
?>

実行結果です。

2014-09-14 00:00:00 昨日
2014-09-15 00:00:00 今日
2014-09-16 00:00:00 明日
2014-09-12 17:35:17 3日前
2014-09-13 17:35:17 2日前
2014-09-14 17:35:17 1日前
2014-09-16 17:35:17 1日後
2014-09-17 17:35:17 2日後
2014-09-18 17:35:17 3日後

週(week) - 先週、今週、来週

<?php
// 週 week
echo date("Y-m-d H:i:s") ." 現在\n";
echo date("Y-m-d H:i:s", strtotime("last week")) ." 先週\n";
echo date("Y-m-d H:i:s", strtotime("this week")) ." 今週\n";
echo date("Y-m-d H:i:s", strtotime("next week")) ." 来週\n";
echo date("Y-m-d H:i:s", strtotime("-3 week")) ." 3週間前\n";
echo date("Y-m-d H:i:s", strtotime("-2 week")) ." 2週間前\n";
echo date("Y-m-d H:i:s", strtotime("-1 week")) ." 1週間前\n";
echo date("Y-m-d H:i:s", strtotime("+1 week")) ." 1週間後\n";
echo date("Y-m-d H:i:s", strtotime("+2 week")) ." 2週間後\n";
echo date("Y-m-d H:i:s", strtotime("+3 week")) ." 3週間後\n\n";
?>

実行結果です。

2014-09-15 17:35:17 現在
2014-09-08 17:35:17 先週
2014-09-15 17:35:17 今週
2014-09-22 17:35:17 来週
2014-08-25 17:35:17 3週間前
2014-09-01 17:35:17 2週間前
2014-09-08 17:35:17 1週間前
2014-09-22 17:35:17 1週間後
2014-09-29 17:35:17 2週間後
2014-10-06 17:35:17 3週間後

月(month) - 先月、今月、来月

<?php
// 月 month
echo date("Y-m-d H:i:s") ." 現在\n";
echo date("Y-m-d H:i:s", strtotime("last month")) ." 先月\n";
echo date("Y-m-d H:i:s", strtotime("this month")) ." 今月\n";
echo date("Y-m-d H:i:s", strtotime("next month")) ." 来月\n";
echo date("Y-m-d H:i:s", strtotime("-3 month")) ." 3ヵ月前\n";
echo date("Y-m-d H:i:s", strtotime("-2 month")) ." 2ヵ月前\n";
echo date("Y-m-d H:i:s", strtotime("-1 month")) ." 1ヵ月前\n";
echo date("Y-m-d H:i:s", strtotime("+1 month")) ." 1ヵ月後\n";
echo date("Y-m-d H:i:s", strtotime("+2 month")) ." 2ヵ月後\n";
echo date("Y-m-d H:i:s", strtotime("+3 month")) ." 3ヵ月後\n\n";
?>

実行結果です。

2014-09-15 17:35:17 現在
2014-08-15 17:35:17 先月
2014-09-15 17:35:17 今月
2014-10-15 17:35:17 来月
2014-06-15 17:35:17 3ヵ月前
2014-07-15 17:35:17 2ヵ月前
2014-08-15 17:35:17 1ヵ月前
2014-10-15 17:35:17 1ヵ月後
2014-11-15 17:35:17 2ヵ月後
2014-12-15 17:35:17 3ヵ月後

年(year) - 去年、今年、来年

<?php
// 年 year
echo date("Y-m-d H:i:s") ." 現在\n";
echo date("Y-m-d H:i:s", strtotime("last year")) ." 去年\n";
echo date("Y-m-d H:i:s", strtotime("this year")) ." 今年\n";
echo date("Y-m-d H:i:s", strtotime("next year")) ." 来年\n";
echo date("Y-m-d H:i:s", strtotime("-3 year")) ." 3年前\n";
echo date("Y-m-d H:i:s", strtotime("-2 year")) ." 2年前\n";
echo date("Y-m-d H:i:s", strtotime("-1 year")) ." 1年前\n";
echo date("Y-m-d H:i:s", strtotime("+1 year")) ." 1年後\n";
echo date("Y-m-d H:i:s", strtotime("+2 year")) ." 2年後\n";
echo date("Y-m-d H:i:s", strtotime("+3 year")) ." 3年後\n";
?>

実行結果です。

2014-09-15 17:35:17 現在
2013-09-15 17:35:17 去年
2014-09-15 17:35:17 今年
2015-09-15 17:35:17 来年
2011-09-15 17:35:17 3年前
2012-09-15 17:35:17 2年前
2013-09-15 17:35:17 1年前
2015-09-15 17:35:17 1年後
2016-09-15 17:35:17 2年後
2017-09-15 17:35:17 3年後

サンプルコード の人気記事

  1. PHPサンプルコード集
  2. PHP + OAuthで Twitter botをつくってみよう

関連記事(一部広告含む)