LoginSignup
5
2

More than 5 years have passed since last update.

【PHP】strtotime("-1 day") == strtotime("---1 day")

Last updated at Posted at 2018-02-22

以下は2018年02月22日に実行したとする。

<?php
// 出力: 2018-02-22 (今日の日付)
echo date("Y-m-d") . "\n";

// 出力: 2018-02-21 (昨日の日付)
echo date("Y-m-d", strtotime("-1 day")) . "\n";

// 出力: 2018-02-23 (マイナスが二個続くと反転して明日になる)
echo date("Y-m-d", strtotime("--1 day")) . "\n";

// 出力: 2018-02-21 (更に反転して昨日の日付に戻る)
echo date("Y-m-d", strtotime("---1 day")) . "\n";

// 出力: true (タイトル回収)
if (strtotime("-1 day") == strtotime("---1 day")) {
    echo "true \n";
} else {
    echo "false \n";
};

// 出力: 2018-02-21 (DateTime オブジェクトでも同様の結果)
echo (new DateTime("---1 day"))->format("Y-m-d");
5
2
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
5
2