php日期时间格式

基础格式化

echo date("Y/m/d H:i:s.u");


$timestamp = 1599257836;
$date = date('Y-m-d H:i:s', $timestamp);
echo $date;
  • 时间转为时间戳
$dateStr = "2021-01-01 12:00:00";
$timestamp = strtotime($dateStr);
echo $timestamp;

日期减一天

date('Y-m-d H:i:s',strtotime('-1 day'));

$date = '2021-07-30';
$previous_day = date('Y-m-d', strtotime($date . ' -1 day'));


$date = '2021-07-30';
$previous_day = date('Y-m-d', strtotime('-1 day', strtotime($date)));

日期加一天

echo "明天:",date('Y-m-d H:i:s',strtotime('+1 day'));




$date = '2022-01-20';
$newDate = date('Y-m-d', strtotime($date . ' +1 day'));

$date = new DateTime('2022-01-20');
$date->modify('+1 day');
$newDate = $date->format('Y-m-d');
  • 其它格式
加一年,一小时,一分钟 ,一月 
echo "明天:",date('Y-m-d H:i:s',strtotime('+1 day'));

echo "明天:",date('Y-m-d H:i:s',strtotime('+1 hour'));

echo "明天:",date('Y-m-d H:i:s',strtotime('+1 minute'));

echo "明天:",date('Y-m-d H:i:s',strtotime('+1 month'));

echo   date("Y-m-d",strtotime("+1 month",strtotime("2012-02-04")));

echo   date("Y-m-d",strtotime("+1 week",strtotime("2011-02-04")));

echo   date("Y-m-d",strtotime("+1 day",strtotime("2011-02-04")));
  • 上个月的最后一天
date('Ymd', strtotime('last day of previous month'))