如何将2016-06-14 21:40:53转换为今天的PHP

时间:2016-06-15 17:56:47

标签: php mysql datetime

如何转换2016-06-14 21:40:53如昨天,2016-06-15 21:40:53作为今天和2016-06-16 21:40:53作为明天在php

2 个答案:

答案 0 :(得分:0)

尝试这个我认为你在寻找

    $date = date('Y-m-d H:i:s');
    $newdate = strtotime ( '-1 day' , strtotime ( $date ) ) ;
    $newdate = date ( 'Y-m-d H:i:s' , $newdate );
     //yesterday
    echo $newdate;

    $tommorow = strtotime ( '+1 day' , strtotime ( $date ) ) ;
    $tommorow = date ( 'Y-m-d H:i:s' , $tommorow );
    echo $tommorow //tommorrow

    echo date('Y-m-d H:i:s') //today

答案 1 :(得分:0)

<?php

$yourDate = new DateTime('2016-06-16 21:40:53');
$yourDate->setTime(0, 0, 0);

$now = new DateTime();
$now->setTime(0, 0, 0);

$diff = $now->diff($yourDate);
$diffInDay = (int)$diff->format('%R%a');

if ($diffInDay == -1) {
    echo 'Yesterday';
} else if ($diffInDay == 0) {
    echo 'Today';
} else if ($diffInDay == 1) {
    echo 'Tomorrow';
} // ... etc

Official documentation