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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度