Do You PHP はてブロ

Do You PHPはてなからはてブロに移動しました

DateTimeクラスのコンストラクタにタイムスタンプを渡す

うへっ、そうだったのか。知らなんだ。。。


One thing which the manual doesn’t show you is that you can also create using a timestamp, very useful if you are working with them already, perhaps you store datetimes that way or accept them as incoming parameters in that format. To do that you can use the following syntax:

$datetime = new DateTime("@{$timestamp}");

今見ると、PHPマニュアルにも書いてありますね。

実際に試してみると、次のような感じ。

$ php -v
PHP 5.2.13 (cli) (built: Feb 26 2010 11:29:29)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2010 Zend Technologies
    with Xdebug v2.0.5, Copyright (c) 2002-2008, by Derick Rethans
$ php -a
Interactive shell

php > $time = new DateTime("@1234567890");
php > echo $time->format('Y/m/d H:i:s');
2009/02/13 23:31:30
php > exit
$