2013-08-22 12:15
浏览 58


This question already has an answer here:

I have 2 unix time, one is todays date and other is expiration time.

  • Todays Time: 1377173245 (2013-08-22 12:07:25)
  • Expiration Time: 1406303166 (2014-07-25 15:46:06)

What I want to achieve is calculate remaining time to expire the listing. In current case, it should show, 11 months -- hours -- minutes -- seconds I didn't find any good solution to calculate the difference. I doubt is it possible to calculate time difference using UNIX time system?

Thank you :-)


图片转代码服务由CSDN问答提供 功能建议

此问题已经存在 这里有一个答案:

  • 如何使用PHP计算两个日期之间的差异? 32个答案

    我有2个unix时间,一个是今天 日期和其他是到期时间。

    • 今日时间:1377173245(2013-08-22 12:07:25)
    • 到期时间 :1406303166(2014-07-25 15:46:06)

      我想要实现的是计算使列表到期的剩余时间。 在目前的情况下,它应该显示, 11个月 - 小时 - 分钟 - 秒我没有找到任何好的解决方案来计算差异。 我怀疑是否可以使用UNIX时间系统计算时差?

      谢谢: - )

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • doupu1727 2013-08-22 12:23

    This is what you're looking for: DateTime::diff

    First create object with 1st date:

    $date = new DateTime('2013-08-22 12:07:25');

    Then use the diff method:

    $diff = $date->diff(new DateTime('2014-07-25 15:46:06'));

    Now if you print $diff variable, you will see the array containing values for hour, minute, day and so on.

    点赞 打赏 评论
  • douzha6055 2013-08-22 12:20

    Try using datetime::diff. Here's the example from the php.net documentation:

    $datetime1 = date_create('2009-10-11');
    $datetime2 = date_create('2009-10-13');
    $interval = date_diff($datetime1, $datetime2);
    echo $interval->format('%R%a days');

    It's easy to create a DateTime object using a timespamp. However, this solution requires PHP 5.3+.

    点赞 打赏 评论
  • duangouyan3328 2013-08-22 12:20

    It is most definitely possible. However, you'll run into problems with months, because there are different number of days in each month, so I'll just do days, hours, minutes, and seconds below.

    $hours=(($expiration-$today)/(60*60))%24; //the modulus operator, gets a remainder

    If you need months, you'll find it easier to use date_diff().

    点赞 打赏 评论
  • douduiyun6125 2013-08-22 12:20

    Use DateTime::diff

    See the doc on http://www.php.net/manual/fr/datetime.diff.php

    点赞 打赏 评论
  • doulu8341 2013-08-22 12:22

    Use DateTime class:


    $datetime1 = new DateTime();
    $datetime2 = new DateTime('2014-07-25 15:46:06');
    $interval = $datetime1->diff($datetime2);
    echo $interval->format('%M months, %H hours, %I minutes, and %S seconds remaining ');


    11 months, 21 hours, 44 minutes, and 20 seconds remaining 
    点赞 打赏 评论

相关推荐 更多相似问题