dongleiwei2182 2014-08-13 09:30
浏览 40

PHP使用小时和分钟减去日期

Hi I've search the internet about this topics and seems can't give me the right ouput? What could I be doing doing wrong? I want to output the remaining days from an expiration period with hours and minutes.

Here's what I've done,

$date_expire = '2014-09-12 23:59:59';  
$date = new DateTime($date_expire);
$now =  new DateTime();
echo $date->diff($now)->format("%d days, %H hours and %i minutes");

I am from the philippines and the current date and time I wrote this is August 13, 2014 5:26pm the ouput gives me 29 days, 12 hours and 30 minutes. Which is think is wrong? Can you help me? thanks!

  • 写回答

2条回答 默认 最新

  • douao2019 2014-08-13 10:25
    关注

    your source code is working fine and it does what you expect.

    yes, on that date was missed 29 days 12 hours and 30 minutes and a few months.

    the problem is that you don't print out the months (:

    here, there is your script with the months included:

    // this is your start date
    $date_expire = "2014-09-12 23:59:59";
    // build a DateTime instance from it
    $date = new DateTime($date_expire);
    // another instance for the date of NOW
    $now =  new DateTime();
    // print the difference with %m (months) included
    echo $date->diff($now)->format("%m months, %d days, %H hours and %i minutes");
    
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向