php 2つの時間差年月日計算方法

8887 ワード



header("Content-type: text/html; charset=utf-8");

/**
 * User: Jack
 * Date: 2017/11/13
 * Time: 16:51
 *       :                 
 */

function diffDate($date1, $date2)
{
    $str = "";
    $datetime1 = new \DateTime($date1);
    $datetime2 = new \DateTime($date2);
    $interval = $datetime1->diff($datetime2);
    $y = $interval->format('%y');
    $m = $interval->format('%m');
    $d = $interval->format('%d');

    if ($y > 0) {
        $str .= $y . ' ';
    }
    if ($m > 0) {
        $str .= $m . '  ';
    }
    if ($d >= 0) {
        $str .= $d . ' ';
    }
    return $str;
}
//     
function getDate1()
{
    $date1 = date('Y-m-d H:i:s', 1509530372);
    return $date1;
}
//    
function getDate2()
{
    $time1 = '2018-05-29 15:34:00';
    $date1 = strtotime($time1);
    return $date1;
}
//  2018-10-29 10:18:46
function getDate3()
{
    $date = date('Y-m-d h:i:s');
    return $date;
}

#     
$time = diffDate('2018-06-30', date('Y-m-d'));
print_r($time);