晨曦啊 晨曦啊

PHP计算两个日期相差的年月日时分秒

in PHP,计算机 文章转载请注明来源!
$start_time = '2018-10-10 10:10:10';
$end_time = '2018-12-28 08:22:22';

get_time($start_time,$end_time);

function get_time($start,$end){
    $start = strtotime($start);
    $end = strtotime($end);
    if ($start > $end){
        $diff_time = $start-$end;
    }else{
        $diff_time = $end-$start;
    }

    $year_t = 3600*24*365;
    $day_t = 3600*24;
    $hours_t = 3600;
    $minute_t = 60;

    $year = floor($diff_time/$year_t);
    $days = floor(($diff_time-$year*$year_t)/$day_t);
    $hours = floor(($diff_time-($year*$year_t)-($days*$day_t))/$hours_t);
    $minute = floor(($diff_time-($year*$year_t)-($days*$day_t)-$hours*$hours_t)/$minute_t);
    $seconds = $diff_time - $minute*$minute_t-$hours*$hours_t-$days*$day_t-$year*$year_t;

    return $year.'年'.$days.'天'.$hours.'小时'.$minute.'分钟'.$seconds.'秒';
}
jrotty WeChat Pay

微信打赏

jrotty Alipay

支付宝打赏

文章二维码

扫描二维码,在手机上阅读!

PHP计算机
发表新评论
前篇 后篇
雷姆
拉姆