【问题标题】:PHP Time calculationPHP时间计算
【发布时间】:2011-05-22 14:30:29
【问题描述】:

假设我有以下 2 个日期,一个开始日期和一个结束日期:

             Year-Month-Day Hours:Minutes:Seconds  

Start Date:  2010-12-03 14:04:41
Expiry Date: 2010-12-06 12:59:59

我怎样才能使用 PHP 减去两个日期并留下如下内容:

差异:-3 天 2 分 18 秒(例如,如果到期日期超过 3 天)。

【问题讨论】:

    标签: php date time


    【解决方案1】:

    这是基于大量在线示例;如果你打开谷歌,你会到处看到类似的代码。

    function timeSince($dateFrom, $dateTo) {
        // array of time period chunks
        $chunks = array(
            array(60 * 60 * 24 * 365 , 'year'),
            array(60 * 60 * 24 * 30 , 'month'),
            array(60 * 60 * 24 * 7, 'week'),
            array(60 * 60 * 24 , 'day'),
            array(60 * 60 , 'hour'),
            array(60 , 'minute'),
        );
    
        $original = strtotime($dateFrom);
        $now      = strtotime($dateTo);
        $since    = $now - $original;
        $message  = ($now < $original) ? '-' : null;
    
        // If the difference is less than 60, we will show the seconds difference as well
        if ($since < 60) {
            $chunks[] = array(1 , 'second');
        }
    
        // $j saves performing the count function each time around the loop
        for ($i = 0, $j = count($chunks); $i < $j; $i++) {
    
            $seconds = $chunks[$i][0];
            $name = $chunks[$i][1];
    
            // finding the biggest chunk (if the chunk fits, break)
            if (($count = floor($since / $seconds)) != 0) {
                break;
            }
        }
    
        $print = ($count == 1) ? '1 ' . $name : $count . ' ' . $name . 's';
    
        if ($i + 1 < $j) {
            // now getting the second item
            $seconds2 = $chunks[$i + 1][0];
            $name2 = $chunks[$i + 1][1];
    
            // add second item if it's greater than 0
            if (($count2 = floor(($since - ($seconds * $count)) / $seconds2)) != 0) {
                $print .= ($count2 == 1) ? ', 1 ' . $name2 : ', ' . $count2 . ' ' . $name2 . 's';
            }
        }
        return $message . $print;
    }
    

    它旨在显示给定时间与当前时间之间的差异,但我做了一些细微的更改以显示两次之间的差异。您可能希望将输出从“以前”的后缀更改为“差异:”的前缀。

    【讨论】:

    • 感谢 El Yobo:不幸的是,我收到了一个致命错误:致命错误:无法重新声明 timesince()(之前在 /usr/local/pem/vhosts/103503/webspace/httpdocs/clients/wp- 中声明) content/themes/discount-codes/page-ending-soon.php:34) 在 /usr/local/pem/vhosts/103503/webspace/httpdocs/clients/wp-content/themes/discount-codes/page-ending-第 34 行的 Soon.php
    • 哈,看起来您已经有一个同名的函数(在这种情况下,只需将我的函数重命名为其他名称),或者您不止一次将包含该函数的文件包含在其中(在这种情况下,使用include_once 而不是include)。
    • 我的错,是在一个循环中 :) - 工作几乎完美!只是一件事,所有结果都返回“之前”。例如:“1 周前”。即使 1 周前应该是未来 1 周?
    • 很高兴为您提供帮助 :) 正如我所说,并不是我所有的代码,当我编写它时,各种在线示例都有帮助,例如viralpatel.net/blogs/2009/06/…
    • 在 $message = ($now > $original) 中? '-' : 空值;它应该是“
    【解决方案2】:

    http://www.php.net/manual/en/datetime.diff.php

    <?php
    $datetime1 = date_create('2009-10-11');
    $datetime2 = date_create('2009-10-13');
    $interval = date_diff($datetime1, $datetime2);
    echo $interval->format('%R%d days');
    //result will be +2 days
    ?>
    

    我希望这就是你要找的。​​p>

    【讨论】:

    • 如果您想要的只是几天,这可以工作,但不会提供 OP 要求的详细信息。
    • echo $interval->format('%R%d 天,%i 分钟和 %s 秒。');
    • 感谢一百万 lves,我的 php 版本尚不支持此代码,但它确实优雅且易于阅读!
    猜你喜欢
    • 2011-09-10
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-29
    • 2020-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多