8
10

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での日付の扱いをまとめてみた

Last updated at Posted at 2016-02-18

phpで日付や時間を扱うときによく使うものをまとめ書き
パラメータ、返り値の説明は、リファレンスのリンクから確認して下さい。

タイムゾーンの設定

date_default_timezone_set('Asia/Tokyo');

関数・クラスのリスト

time() / microtime()

現在のUnix タイムスタンプを、秒単位 / マイクロ秒単位で取得します。

http://php.net/manual/ja/function.time.php
http://php.net/manual/ja/function.microtime.php

var_dump(time());			// int(1455605830)
var_dump(microtime(true));	// float(1455605830.3951)
var_dump(microtime(false));	// string(21) "0.39511400 1455605830"

date()

http://php.net/manual/ja/function.date.php

var_dump(date('Y/m/d H:i:s', 0));			// string(19) "1970/01/01 09:00:00"
var_dump(date('Y月n日j G時i分s秒', 0));	// string(22) "1970月1日1 9時00分00秒"
$week_str = ['日', '月', '火', '水', '木', '金', '土'];
var_dump(date('w', 0));						// string(1) "4"
var_dump($week_str[date('w', 0)]);			// string(2) "木"

strtotime()

http://php.net/manual/ja/function.strtotime.php

var_dump(strtotime("2010-9-10"));							// int(1455607117) 日付指定
var_dump(strtotime("2010-9-10 00:00:11"));					// int(1284044400) 日付指定
var_dump(strtotime("10 September 2010"));					// int(1284044411) 日付指定
var_dump(strtotime('now'));									// int(1284044400) 今
var_dump(strtotime("+1 day"));								// int(1455693517) 1日後
var_dump(strtotime("+1 week"));								// int(1456211917) 1週間後
var_dump(strtotime("+1 week 2 days 4 hours 2 seconds"));	// int(1456399119) 1週間と2日と4時間と2秒後
var_dump(strtotime("next Thursday"));						// int(1455721200) 次の木曜日
var_dump(strtotime("last Monday"));							// int(1455462000) 前の月曜日
strtotime( "+2 week sunday" );									//「次の次の週」に含まれる日曜日 ※「次の次の日曜」では無いので注意
ISO 8601では月曜日曜なので、「次の週になった時点で日曜が過ぎる
次の次の日曜だとこううるう秒があったら知らない
strtotime( "next sunday") + 7*24*60*60;

DateTimeクラス

http://php.net/manual/ja/book.datetime.php
http://php.net/manual/ja/class.datetime.php

$date = new DateTime();							// now
$date = new DateTime('2000-01-01');				// 日付のみ指定
$date = new DateTime('2000-01-01 13:10:00');	// 日付&時間を指定
$date->format('Y/m/d H:i:s');	// フォーマットを指定
$date->getTimestamp();			// UNIXタイムスタンプで取得

日付の比較

// case 1
$today  = new DateTime('2014-10-12');
$target = new DateTime('2014-10-2');
var_dump($today == $target); // bool(false)
var_dump($today > $target); // bool(true)
var_dump($today < $target); // bool(false)

// case 2
$today  = new DateTime('2014-10-12');
$target = new DateTime('2014-10-12');
var_dump($today == $target); // bool(true)
var_dump($today > $target); // bool(false)
var_dump($today < $target); // bool(false)

// case 3
$today  = new DateTime('2014-10-12 00:00:00');
$target = new DateTime('2014-10-12 00:00:11');
var_dump($today == $target); // bool(false)
var_dump($today > $target); // bool(false)
var_dump($today < $target); // bool(true)

経過時間の比較

$today  = new DateTime('2014-10-12');
$target = new DateTime('2014-10-2');

$date_diff = $today->diff($target);
echo $date_diff->format('%r%a');    // 経過総日数 10

DateTime::diff

日付と時刻の書式

date() が理解できる書式指定文字列
主にdate(), DateTime::format() などに渡すフォーマット
http://php.net/manual/ja/function.date.php

サポートする日付と時刻の書式
主にstrtotime(), new DateTime() に渡すフォーマット
http://php.net/manual/ja/datetime.formats.php

DateTime::diffの返り値のクラス(DateInterval)のformat()メソッドに渡すフォーマット
http://php.net/manual/ja/dateinterval.format.php

オマケ sleep() / usleep() / time_nanosleep()

直接関係無いけど、スリープ系

http://php.net/manual/ja/function.sleep.php
http://php.net/manual/ja/function.usleep.php
http://php.net/manual/ja/function.time-nanosleep.php

8
10
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
8
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?