It seems that a pipe ('|') option in formating string works only with PHP version 5.3.8 and newer. We had an issue with it on versions 5.3.2, 5.3.3, 5.3.6. Yet it was fine with 5.3.8 and 5.3.10.
By short example:
<?php
$timezone = new DateTimeZone('UTC');
$dateTime = DateTime::createFromFormat('dmY|', '01011972', $timezone);
//$dateTime is FALSE in PHP v <5.3.8
?>
Instead we used a workaround:
<?php
$dateTime = DateTime::createFromFormat('dmY', '01011972', $timezone);
$dateTime->format('Y-m-d 00:00:00');
?>
which works fine.
DateTime::createFromFormat
date_create_from_format
(PHP 5 >= 5.3.0)
DateTime::createFromFormat -- date_create_from_format — 指定した書式でフォーマットした新しい DateTime オブジェクトを返す
説明
オブジェクト指向型
$format
, string $time
[, DateTimeZone $timezone
] )手続き型
指定した書式でフォーマットした新しい DateTime オブジェクトを返します。
パラメータ
-
format -
書式を文字列で渡します。以下の書式オプションを参照ください。 大半は、date() で使える文字と同じです。
formatパラメータに渡せる文字format文字説明 取りうる値の例 日 --- --- d および j 2桁の日付。先頭のゼロを含むものと含まないもの 01 から 31 あるいは 1 から 31 D および l 曜日を表す文字列 Mon から Sun あるいは Sunday から Saturday S 日付の後につける英語の接尾辞。二文字。処理中には無視されます。 st、nd、rd あるいは th z 年始からの通算日数 (最初は 0) 0 から 365 月 --- --- F および M 月を表す文字列。January あるいは Sept など January から December あるいは Jan から Dec m および n 月を表す数値。先頭のゼロを含むものと含まないもの 01 から 12 あるいは 1 から 12 年 --- --- Y 4 桁の数値で表した年 例: 1999 あるいは 2003 y 2 桁の数値で表した年 例: 99 あるいは 03 時刻 --- --- a および A 午前および午後 am あるいは pm g および h 12 時間制での時間。先頭のゼロを含むものと含まないもの 1 から 12 あるいは 01 から 12 G and H 24 時間制での時間。先頭のゼロを含むものと含まないもの 0 から 23 あるいは 00 から 23 i 分。先頭のゼロを含む 00 から 59 s 秒。先頭のゼロを含む 00 から 59 u マイクロ秒 (最大 6 桁) 例: 45, 654321 タイムゾーン --- --- e、O、 P および T タイムゾーン識別子、UTC からの時差 (時間単位)、 UTC からの時差 (コロン区切りでの時間と分)、そしてタイムゾーンの短縮形 例: UTC、GMT、 Atlantic/Azores あるいは +0200 あるいは +02:00 あるいは EST、MDT 完全な日付/時刻 --- --- U Unix エポック (January 1 1970 00:00:00 GMT) からの経過秒数 例: 1292177455 空白および区切り --- --- (空白) 空白 1 文字あるいはタブ 1 文字 例: # 次の区切り文字のいずれか: ;, :, /, ., ,, -, ( あるいは ) 例: / ;, :, /, ., ,, -, ( あるいは ) 指定した文字 例: - ? ランダムなバイト 例: ^ (UTF-8 文字の場合は複数の ? が必要になるでしょう。この場合、おそらく * を使うと要望が満たせるはずです) * 次の区切り文字あるいは数字までのランダムなバイト列 例: Y-*-d の中の * は、文字列 2009-aWord-08 の中の aWord にマッチします ! すべてのフィールド (年、月、日、時、分、秒、マイクロ秒およびタイムゾーン情報) を Unix エポックにリセットする ! がなければ、すべてのフィールドは現在の日時に設定されます。 | まだパースされていないすべてのフィールド (年、月、日、時、分、秒、マイクロ秒およびタイムゾーン情報) を Unix エポックにリセットする Y-m-d| は、文字列をパースした結果から年月日を設定し 時分秒には 0 を設定します。 + この文字があると、文字列のそれ以降のデータではエラーが発生せず、 かわりに警告を発生させる それ以降のデータが存在したかどうかを調べるには DateTime::getLastErrors() を使います。 書式文字列の中に解釈不能な文字が含まれていると処理は失敗し、 返り値にはエラーメッセージが付加されます。エラーメッセージを調べるには DateTime::getLastErrors() を使います。
formatに文字 ! が含まれない場合は、作成した時刻値のうちformatで指定されていない部分を 現在のシステム時刻で初期化します。formatに文字 ! が含まれる場合は、作成した時刻値のうちformatで指定されていない部分と ! の左側の部分を Unix エポックの対応する箇所の値で初期化します。Unix エポックは 1970-01-01 00:00:00 です。
-
time -
時刻を表す文字列。
-
timezone -
指定したいタイムゾーンを表す DateTimeZone オブジェクト。
timezoneを省略し、かつtimeにタイムゾーンが含まれない場合は、 現在のタイムゾーンを使います。注意:
timeパラメータが UNIX タイムスタンプ (例: 946684800) だったりタイムゾーンを含んでいたり (例: 2010-01-28T15:00:00+02:00) する場合は、timezoneパラメータや現在のタイムゾーンは無視します。
返り値
新しい DateTime のインスタンス、失敗した場合に FALSE を返します。
例
例1 DateTime::createFromFormat() の例
オブジェクト指向型
<?php
$date = DateTime::createFromFormat('j-M-Y', '15-Feb-2009');
echo $date->format('Y-m-d');
?>
手続き型
<?php
$date = date_create_from_format('j-M-Y', '15-Feb-2009');
echo date_format($date, 'Y-m-d');
?>
上の例の出力は以下となります。
2009-02-15
例2 DateTime::createFromFormat() の複雑な例
<?php
echo 'Current time: ' . date('Y-m-d H:i:s') . "\n";
$format = 'Y-m-d';
$date = DateTime::createFromFormat($format, '2009-02-15');
echo "Format: $format; " . $date->format('Y-m-d H:i:s') . "\n";
$format = 'Y-m-d H:i:s';
$date = DateTime::createFromFormat($format, '2009-02-15 15:16:17');
echo "Format: $format; " . $date->format('Y-m-d H:i:s') . "\n";
$format = 'Y-m-!d H:i:s';
$date = DateTime::createFromFormat($format, '2009-02-15 15:16:17');
echo "Format: $format; " . $date->format('Y-m-d H:i:s') . "\n";
$format = '!d';
$date = DateTime::createFromFormat($format, '15');
echo "Format: $format; " . $date->format('Y-m-d H:i:s') . "\n";
?>
上の例の出力は、 たとえば以下のようになります。
Current time: 2010-04-23 10:29:35 Format: Y-m-d; 2009-02-15 10:29:35 Format: Y-m-d H:i:s; 2009-02-15 15:16:17 Format: Y-m-!d H:i:s; 1970-01-15 15:16:17 Format: !d; 1970-01-15 00:00:00
参考
- DateTime::__construct() - 新しい DateTime オブジェクトを返す
- DateTime::getLastErrors() - 警告およびエラーを返す
- checkdate() - グレゴリオ暦の日付/時刻の妥当性を確認します
- strptime() - strftime が生成した日付/時刻をパースする
Beware specifying a timezone in the format as it will take precedence over the DateTimeZone object.
<?php
$timezone = "UTC"; // or any other valid name for a timezone
$d= DateTime::createFromFormat("Y-m-d H:i:s T","2011-11-06 00:00:00 EDT",new DateTimeZone($timezone));
echo $d->format("Y-m-d H:i:s T - U");
// returns "2011-11-06 00:00:00 EDT - 1320552000"
// specifying $timezone = "Pacific/Honolulu"; would return the same string
?>
This gets hairy when you are playing with transition from summer time to winter time! For instance, in Toronto, the time change happens on 2011-11-06. One second after 01:59:59 (EDT), the time becomes 01:00:00 (EST), or 1320559200 in Unix timestamp.
However, notice the following:
<?php
$d = DateTime::createFromFormat("Y-m-d H:i:s","2011-11-06 01:00:00",new DateTimeZone("EST"));
echo $d->format("Y-m-d H:i:s T U");
// returns "2011-11-06 01:00:00 EDT 1320555600" instead of "2011-11-06 01:00:00 EST 1320559200"
// so the correct way is to do:
$d = DateTime::createFromFormat("Y-m-d H:i:s T","2011-11-06 01:00:00 EST",new DateTimeZone($timezone)); // set $timezone to any valid string for DateTimeZone, it doesn't matter
echo $d->format("Y-m-d H:i:s T U");
// returns "2011-11-06 01:00:00 EST - 1320559200" as wanted
?>
In order to use a DateTimeZone, don't enter one of the DateTimeZone::Europe, DateTimeZone::Asia etc. constants, but create a DateTimeZone object with verbal timezone name passed as a string:
<?php
$eventDate = DateTime::createFromFormat('m/d/y h:i', '02/26/11 08:00', new DateTimeZone('Europe/Warsaw'));
echo date_format($eventDate, 'Y-m-d'); //prints "2011-02-26"
?>
You can use the ISO8601 constant instead:
$date = DateTime::createFromFormat(DateTime::ISO8601, date("c"))
You can view the other constants at:
http://php.net/manual/en/class.datetime.php
This function seems to not support ISO 8601 format yet.
This fixe solves the problems:
use format : "Y-m-d\TH:i:sP"
