Note that although a milliseconds portion in ISO8601 timestamps is legal, PHP cannot parse them and will throw an exception. No parser in PHP can parse it.
DateTime::__construct
date_create
(PHP 5 >= 5.2.0)
DateTime::__construct -- date_create — 新しい DateTime オブジェクトを返す
説明
オブジェクト指向型
手続き型
新しい DateTime オブジェクトを返します。
パラメータ
-
time -
日付/時刻 文字列。有効な書式については 日付と時刻の書式 で説明しています。
ここに
NULLを指定して$timezoneパラメータを使うと、現在時刻を取得できます。 -
timezone -
指定したいタイムゾーンを表す DateTimeZone オブジェクト。
$timezoneを省略した場合は、 現在のタイムゾーンを使います。注意:
$timeパラメータが UNIX タイムスタンプ (@946684800 など) であったりタイムゾーンつきで指定した場合 (2010-01-28T15:00:00+02:00 など) は、$timezoneパラメータや現在のタイムゾーンは無視されます。
返り値
新しい DateTime のインスタンスを返します。
手続き型 の場合は、失敗したときに FALSE を返します。
エラー / 例外
エラーがあった場合は Exception を発生させます。
変更履歴
| バージョン | 説明 |
|---|---|
| 5.3.0 | 無効な日付を指定した場合に例外がスローされるようになりました。 これまではエラーが発生していました。 |
例
例1 DateTime::__construct() の例
オブジェクト指向型
<?php
try {
$date = new DateTime('2000-01-01');
} catch (Exception $e) {
echo $e->getMessage();
exit(1);
}
echo $date->format('Y-m-d');
?>
手続き型
<?php
$date = date_create('2000-01-01');
if (!$date) {
$e = date_get_last_errors();
foreach ($e['errors'] as $error) {
echo "$error\n";
}
exit(1);
}
echo date_format($date, 'Y-m-d');
?>
上の例の出力は以下となります。
2000-01-01
例2 DateTime::__construct() の複雑な例
<?php
// そのコンピュータのタイムゾーンでの日時の指定
$date = new DateTime('2000-01-01');
echo $date->format('Y-m-d H:i:sP') . "\n";
// 指定したタイムゾーンでの日時の指定
$date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "\n";
// そのコンピュータのタイムゾーンでの現在日時
$date = new DateTime();
echo $date->format('Y-m-d H:i:sP') . "\n";
// 指定したタイムゾーンでの現在日時
$date = new DateTime(null, new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "\n";
// UNIX タイムスタンプの使用例。結果のタイムゾーンは UTC となることに注意しましょう。
$date = new DateTime('@946684800');
echo $date->format('Y-m-d H:i:sP') . "\n";
// 存在しない値は繰り上がります
$date = new DateTime('2000-02-30');
echo $date->format('Y-m-d H:i:sP') . "\n";
?>
上の例の出力は、 たとえば以下のようになります。
2000-01-01 00:00:00-05:00 2000-01-01 00:00:00+12:00 2010-04-24 10:24:16-04:00 2010-04-25 02:24:16+12:00 2000-01-01 00:00:00+00:00 2000-03-01 00:00:00-05:00
参考
- DateTime::createFromFormat() - 指定した書式でフォーマットした新しい DateTime オブジェクトを返す
- DateTimeZone::__construct() - 新しい DateTimeZone オブジェクトを作成する
- 日付と時刻の書式
- date.timezone ini 設定
- date_default_timezone_set() - スクリプト中の日付/時刻関数で使用されるデフォルトタイムゾーンを設定する
- DateTime::getLastErrors() - 警告およびエラーを返す
- checkdate() - グレゴリオ暦の日付/時刻の妥当性を確認します
sebi at timewaster dot de
24-Feb-2012 01:50
cHao
22-Dec-2011 07:17
There's a reason for ignoring the time zone when you pass a timestamp to __construct. That is, UNIX timestamps are by definition based on UTC. @1234567890 represents the same date/time regardless of time zone. So there's no need for a time zone at all.
joel dot kallman at gmail dot com
23-May-2011 06:14
A definite "gotcha" (while documented) that exists in the __construct is that it ignores your timezone if the $time is a timestamp. While this may not make sense, the object does provide you with methods to work around it.
<?php
// New Timezone Object
$timezone = new DateTimeZone('America/New_York');
// New DateTime Object
$date = new DateTime('@1306123200', $timezone);
// You would expect the date to be 2011-05-23 00:00:00
// But it actually outputs 2011-05-23 04:00:00
echo $date->format('Y-m-d H:i:s');
// You can still set the timezone though like so...
$date->setTimezone($timezone);
// This will now output 2011-05-23 00:00:00
echo $date->format('Y-m-d H:i:s');
?>
Tim Strehle
05-May-2010 03:50
"The $timezone parameter and the current timezone are ignored when the $time parameter […] is a UNIX timestamp."
Watch out – this means that these two are NOT equivalent, they result in different timezones (unless your current timezone is GMT):
<?php
$d = new DateTime(); $d->setTimestamp($t);
echo $o->format('O');
// +0200
$d = new DateTime('@' . $t);
echo $o->format('O');
// +0000
?>
kendsnyder at gmail dot com
12-Jan-2010 08:37
Also forgot to mention, that MySQL "zeroed" dates do not throw an error but produce a non-sensical date:
<?php
$d = new DateTime("0000-00-00");
$d->format("Y-m-d"); // "-0001-11-30"
?>
Another good reason to write your own class that extends from DateTime.
kendsnyder at gmail dot com
12-Jan-2010 08:24
The theoretical limits of the date range seem to be "-9999-01-01" through "9999-12-31" (PHP 5.2.9 on Windows Vista 64):
<?php
$d = new DateTime("9999-12-31");
$d->format("Y-m-d"); // "9999-12-31"
$d = new DateTime("0000-12-31");
$d->format("Y-m-d"); // "0000-12-31"
$d = new DateTime("-9999-12-31");
$d->format("Y-m-d"); // "-9999-12-31"
?>
Dates above 10000 and below -10000 do not throw errors but produce weird results:
<?php
$d = new DateTime("10019-01-01");
$d->format("Y-m-d"); // "2009-01-01"
$d = new DateTime("10009-01-01");
$d->format("Y-m-d"); // "2009-01-01"
$d = new DateTime("-10019-01-01");
$d->format("Y-m-d"); // "2009-01-01"
?>
