获取一个想要的日期,时间

  很重要的函数:strtotime()

  比如获取某个时间一周前日期应该是什么?

1 //当前时间
2 $nowDate = "2017-01-01 00:00:00";
3 $lastWeek = date('Y-m-d H:i:s' , strtotime('-1 week ', strtotime($nowDate)));
4 echo '上周的日期时间为:'.$lastWeek;

  获取一天前,一月前,一年前方法也是如此

//1天前
$lastDayDate = date("Y-m-d H:i:s", strtotime('-1 day', strtotime($dtE)));
//1月前
$lastMonthDate = date("Y-m-d H:i:s", strtotime('-1 month', strtotime($dtE)));
//1年前
$lastYearDate = date("Y-m-d H:i:s", strtotime('-1 year', strtotime($dtE)));

  注意:strtotime()函数,

    1.如果只有一个参数,即获取某个日期的时间戳;

    2.如果有两个参数,则第二个参数应该是时间戳格式;

posted @ 2017-11-17 12:05  vlson  阅读(289)  评论(0编辑  收藏  举报