17
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

phpで日付操作を行うための複数の方法

Posted at

いくつかの日付の操作方法について、思いつく限り書いてみたが、複数のやり方があってどれを使えばいいのか迷ってしまう・・・

今まで date, strtotime 使ってたけど、これからは DateTime とかの日付関数を使う予定。

日付の書式を変更

$format2 = DateTime::createFromFormat("Y-m-d H:i:s", "2012-02-09 10:15:01");
echo $format2->format(Datetime::RSS);
// -> Thu, 09 Feb 2012 10:15:01 +0900

$datetime = new DateTime();
$format1 = $datetime->createFromFormat("Y-m-d H:i:s", "2012-02-09 10:15:01");
echo $format1->format(Datetime::RSS);
// -> Thu, 09 Feb 2012 10:15:01 +0900

echo date(Datetime::RSS, strtotime("Thu Feb 09 10:15:01 JST 2012"));
// -> Thu, 09 Feb 2012 10:15:01 +0900

// 日付の差を求める
$date1 = date_create("2012-02-01");
$date2 = date_create("2012-02-10");
echo date_diff($date1, $date2)->days;
// -> 9

$date1 = new DateTime("2012-02-01");
$date2 = new DateTime("2012-02-10");
echo $date1->diff($date2)->days;
// -> 9

echo (strtotime("2012-02-10") - strtotime("2012-02-01")) / 86400;
// -> 9

list($year1, $month1, $day1) = explode("-", "2012-02-10");
list($year2, $month2, $day2) = explode("-", "2012-02-01");
$date1 = mktime(0, 0, 0, $month1, $day1, $year1);
$date2 = mktime(0, 0, 0, $month2, $day2, $year2);
echo ($date1 - $date2) / 86400;
// -> 9

間違ってでも最後の方法は使わないでください!
** **

UNIXタイム

$date = date_create("2012-02-01");
echo date_timestamp_get($date);
// -> 1328022000

$date = new DateTime("2012-02-01");
echo $date->getTimestamp();
// -> 1328022000

echo strtotime("2012-02-01");
// -> 1328022000

** **

日付計算

$date = date_create("2012-02-01");
echo date_modify($date, "+5 day")->format("Y-m-d");
// -> 2012-02-06

$date = new DateTime("2012-02-01");
echo $date->modify("+5 day")->format("Y-m-d");
// -> 2012-02-06

$date = date_create("2012-02-01");
echo date_add($date, date_interval_create_from_date_string('+5 days'))->format("Y-m-d");
// -> 2012-02-06

$date = new DateTime("2012-02-01");
echo $date->add(new DateInterval('P5D'))->format("Y-m-d");
// -> 2012-02-06

echo date("Y-m-d", strtotime("+5 day", strtotime("2012-02-01")));
// -> 2012-02-06

list($year, $month, $day) = explode("-", "2012-02-01");
echo date("Y-m-d", mktime(0, 0, 0, $month, $day+5, $year));
// -> 2012-02-06

最後の方法は使わないでください!
文字列関数を使うやり方をあげたら切りがなさそう
素直に日付関数を使った方が分かりやすい
** **

今週の月曜日の日付

$date = date_create("2012-02-08");
echo date_modify($date, "this week Monday")->format("Y-m-d");
// -> 2012-02-02

** **

先週の月曜日の日付

$date = date_create("2012-02-08");
echo date_modify($date, "last week Monday")->format("Y-m-d");
// -> 2012-01-30

** **

次週の月曜日の日付

$date = date_create("2012-02-08");
echo date_modify($date, "+1 Monday")->format("Y-m-d");
// -> 2012-02-13

** **

次の月曜日からさらに2週間後の月曜日の日付

$date = date_create("2012-02-08");
echo date_modify($date, "monday +2 weeks")->format("Y-m-d");
// -> 2012-02-27

このように複数の計算を一度に指定することも可能!
** **

来月の初日時を取得

$date = date_create("2012-02-08");
echo date_modify($date, "first day of next month")->format("Y-m-d(D)");
// -> 2012-03-01(Thu)

** **

これらのキーワードを組み合わせて日付の計算ができる

- sec
- secs
- second
- seconds
- min
- mins
- minute
- minutes
- hour
- hours
- day
- days
- week
- weeks
- fortnight
- fortnights
- forthnight
- forthnights 
- month
- months
- year
- years
- monday
- mon
- tuesday
- tue
- wednesday
- wed
- thursday
- thu
- friday
- fri
- saturday
- sat
- sunday
- sun
- weekday
- weekdays

- first
- next
- second
- third
- fourth
- fifth
- sixth
- seventh
- eight
- eighth
- ninth
- tenth
- eleventh
- twelfth
- last
- previous
- this

** **
他にも便利なものがあったら是非コメントください

17
24
4

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
17
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?