duandanxiu6965 2017-10-05 08:42
浏览 24
已采纳

获取日期返回零之间的差异

I have a date time in 'Y-m-d H:i:s', and i tried to substract the now date with the defined date +1 day to get remaining time in hours, minutes and seconds:

$time = '2017-10-05 14:54:03';
$now =  date('Y-m-d H:i:s');
$endTransaction = date('Y-m-d H:i:s', strtotime($time. ' + 1 day'));
$dteDiff  = $endTransaction - $now;
echo $dteDiff;

but i always get 0 as the result

  • 写回答

4条回答 默认 最新

  • douweng7083 2017-10-05 08:51
    关注

    You are doing it wrong. The date function returns string so PHP is not able to compare anything. Try with the DateTime class instead. Its diff method returns the DateInterval object with some public properties, like the days property among others, which is the positive integer number (rounded down) of days between two dates:

    $now = new \DateTime();
    $endTransaction = (new \DateTime('2017-12-05 14:54:03'))->modify('+1 day');
    
    $diff = $endTransaction->diff($now);
    
    printf(
        'Difference in days: %d, hours: %d, minutes: %d, seconds: %d',
         $diff->days,
         $diff->h,
         $diff->m,
         $diff->s
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器