php 两个时间相差 年 月 日 计算方法

发布时间:2019-08-08 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php 两个时间相差 年 月 日 计算方法脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

第一种方法,使用PHP类库实现

/**
 * function:计算两个日期相隔多少年,多少月,多少天
 * param string $date1[格式如:2011-11-5]
 * param string $date2[格式如:2012-12-01]
 * return array array('年','月','日');
 */
function diffDate($date1,$date2)
{
    $datetime1 = new DateTime($date1);
    $datetime2 = new DateTime($date2);
    $interval = $datetime1->diff($datetime2);
    $time['y']         = $interval->format('%Y');
    $time['m']         = $interval->format('%m');
    $time['d']         = $interval->format('%d');
    $time['h']         = $interval->format('%H');
    $time['i']         = $interval->format('%i');
    $time['s']         = $interval->format('%s');
    $time['a']         = $interval->format('%a');    // 两个时间相差总天数
    return $time;
}


# 使用实例
$sss = diffDate('2015-12-25 12:30:30', '2015-12-26 15:00:00');
print_r($sss);

# 输出
Array
(
    [y] => 00
    [m] => 0
    [d] => 1
    [h] => 02
    [i] => 29
    [s] => 30
    [a] => 1
)

第二种方法,使用函数实现(还在测试中....)

// 计算两个时间相差(时分秒)
$aaa = strtotime('2015-12-26 16:33:33');
$bbb = $aaa-time();
echo gmstrftime('%Y %m %d %H:%M:%S',$aaa);
exit;

$left_time = 86400 - time()+strtotime($orderInfo['create_time']);
echo:gmstrftime('%H:%M:%S',$left_time);

脚本宝典总结

以上是脚本宝典为你收集整理的php 两个时间相差 年 月 日 计算方法全部内容,希望文章能够帮你解决php 两个时间相差 年 月 日 计算方法所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。