LoginSignup
0
0

More than 3 years have passed since last update.

CakePHP3 Cake\I18n\Timeのmodify()に気をつける

Last updated at Posted at 2020-01-12

TL;DR

  • Cake\I18n\Timeクラスが用意するmodifyメソッドの挙動に関して
    (以下、modifyと呼ぶ)
  • modifyは、生成されたインスタンス自身を上書く習性がある(ようだ)
    • 以下のサンプルコードを参考にして欲しい

サンプルコード

$time = new Cake\I18n\Time();
for ($i = 0; $i < 3; $i++) {
    // 主力結果を見易くするためにあえて改行コードを付ける
    echo $time->i18nFormat('yyyy-MM') . "\n";
    echo $time->modify('next month')->i18nFormat('yyyy-MM') . "\n";
}

/*
 * 期待値
 *
 * 2020-01
 * 2020-02
 * 2020-01
 * 2020-02
 * 2020-01
 * 2020-02
 *
 */

/*
 * 実行値
 *
 * 2020-01
 * 2020-02
 * 2020-02
 * 2020-03
 * 2020-03
 * 2020-04
 *
 */

公式ドキュメントはこちら: https://book.cakephp.org/3/ja/core-libraries/time.html

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