0
0

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 3 years have passed since last update.

PHPにてDateTimeクラスのインスタンス化とフォーマットを1行で実行

Last updated at Posted at 2021-02-16

はじめに

DateTimeクラス利用の際、インスタンス化とformatを1行でもかけることを本日知りました。

概要

インスタンス化してそのままformat

$datetime = (new DateTime())->format('Y-m-d H:i:s');
echo $datetime; // 2021-02-16 18:31:22

今まではフォーマット済みの値が入った変数を何箇所かで利用する場合はこんな感じにやってました。。

$datetime = new DateTime();
$datetime = $datetime->format('Y-m-d H:i:s');
echo $datetime;

おわりに

もしかしたらみなさん当たり前のように使っているのかもしれませんが、個人的に知れて嬉しかったので残しておきます。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?