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 文字列を時刻に変換

Last updated at Posted at 2020-02-20

###使う機会があったのでメモしておきます。

指定の時間からある日時を足し引きしたい場合

qiita.php
$str = "10";
$int = intval($str);
$datetime = "2020/02/20 13:00:00";

//10秒前 2020-02-20 12:59:50
echo date("Y-m-d H:i:s",strtotime($datetime . "-" . $str . "second"));
echo "<br>";
//10分後 2020-02-20 13:10:00
echo date("Y-m-d H:i:s",strtotime($datetime . "+" . $int . "minute"));
echo "<br>";
//10時間前 2020-02-20 03:00:00
echo date("Y-m-d H:i:s",strtotime($datetime . "-" . $str . "hour"));
echo "<br>";
//10日後 2020-03-01 13:00:00
echo date("Y-m-d H:i:s",strtotime($datetime . "+" . $int . "day"));
echo "<br>";
//10週間前 2019-12-12 13:00:00
echo date("Y-m-d H:i:s",strtotime($datetime . "-" . $str . "week"));
echo "<br>";
//10ヶ月後 2020-12-20 13:00:00
echo date("Y-m-d H:i:s",strtotime($datetime . "+" . $int . "month"));
echo "<br>";
//10年前 2010-02-20 13:00:00
echo date("Y-m-d H:i:s",strtotime($datetime . "-" . $str . "year"));
echo "<br>";

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?