Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

以下は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");
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした