###获取当前时间字符串/时间戳

echo date("Y-m-d h:i:sa");
// 2018-01-15 06:12:15am

echo time();
// 1515996628

时间字符串转时间戳

​strtotime​​ 是个很聪明的函数,可以接收很多时间格式,例如:

<?php
echo strtotime("now"), "\n";
echo strtotime("10 September 2000"), "\n";
echo strtotime("+1 day"), "\n";
echo strtotime("+1 week"), "\n";
echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
echo strtotime("next Thursday"), "\n";
echo strtotime("last Monday"), "\n";
?>
echo strtotime('2018-01-15 06:12:15am');
// 1515996735

时间戳转时间字符串

<?php
echo date("Y-m-d h:i:s", 1515996735);
// 2018-01-15 06:12:15
echo "今天:".date("Y-m-d")."<br>";
echo "昨天:".date("Y-m-d",strtotime("-1 day")), "<br>";
echo "明天:".date("Y-m-d",strtotime("+1 day")). "<br>";
echo "一周后:".date("Y-m-d",strtotime("+1 week")). "<br>";
echo "一周零两天四小时两秒后:".date("Y-m-d G:H:s",strtotime("+1 week 2 days 4 hours 2 seconds")). "<br>";
echo "下个星期四:".date("Y-m-d",strtotime("next Thursday")). "<br>";
echo "上个周一:".date("Y-m-d",strtotime("last Monday"))."<br>";
echo "一个月前:".date("Y-m-d",strtotime("last month"))."<br>";
echo "一个月后:".date("Y-m-d",strtotime("+1 month"))."<br>";
echo "十年后:".date("Y-m-d",strtotime("+10 year"))."<br>";

获取前N天N月N年的时间字符串/时间戳

// 一天前
// 时间戳
echo mktime(0, 0, 0, date('m'), date('d')-1, date('Y')), "\n";
// 时间字符串
echo date("Y-m-d H:i:s", strtotime("-1 day")), "\n";

// 一周前
// 时间戳
echo mktime(0, 0, 0, date('m'), date('d')-7, date('Y')), "\n";
// 时间字符串
echo date("Y-m-d H:i:s", strtotime("-7 day")), "\n";

// 一月前
// 时间戳
echo mktime(0, 0, 0, date('m')-1, date('d'), date('Y')), "\n";
// 时间字符串
echo date("Y-m-d H:i:s", strtotime("-1 month")), "\n";

// 一年前
// 时间字符串
echo mktime(0, 0, 0, date('m'), date('d'), date('Y')-1), "\n";
// 时间日期
echo date("Y-m-d H:i:s", strtotime("-1 year")), "\n";