PHP时间计算 [英] PHP Time calculation

查看:109
本文介绍了PHP时间计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有以下2个日期,开始日期和结束日期:

 年 - 月 - 日小时:分钟:秒

开始日期:2010-12-03 14:04:41
到期日:2010-12-06 12:59:59

我如何使用PHP减去两个日期,并留下如下:



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

解决方案

这是基于许多在线示例;你会看到类似的代码,如果你有你的谷歌。

  function timeSince($ dateFrom,$ dateTo){
//时间段数组
$ chunks = array(
array(60 * 60 * 24 * 365,'year'),
array(60 * 60 * 24 * 30,'month'),
数组(60 * 60 * 24 * 7,'week'),
数组(60 * 60 * 24,'day'),
数组* 60,'hour'),
数组(60,'分钟),
);

$ original = strtotime($ dateFrom);
$ now = strtotime($ dateTo);
$ since = $ now - $ original;
$ message =($ now< $ original)? '-' : 空值;

//如果差异小于60,我们将显示秒差异
if($ since< 60){
$ chunks [] = array 1秒');
}

// $ j每次循环执行计数功能
($ i = 0,$ j = count($ chunks); $ i< ; $ j; $ i ++){

$ seconds = $ chunks [$ i] [0];
$ name = $ chunks [$ i] [1];

//找到最大的块(如果块适合,中断)
if(($ count = floor($ since / $ seconds))!= 0){
打破;
}
}

$ print =($ count == 1)? '1'。 $ name:$ count。 。 $名称。 ''

if($ i + 1< $ j){
//现在获得第二个项目
$ seconds2 = $ chunks [$ i + 1] [0];
$ name2 = $ chunks [$ i + 1] [1];

//添加第二项如果大于0
if(($ count2 = floor(($ since - ($ seconds * $ count))/ $ seconds2))!= 0 ){
$ print。=($ count2 == 1)? ',1'。 $ name2:','。 $ count2。 。 $ name2。 ''
}
}
返回$消息。 $ print;
}

它旨在显示给定时间与当前时间之间的差异,但是我做了一些微小的变化来显示两次之间的区别。您可能希望将以前的后缀的输出更改为Difference:的前缀。


Say I have the following 2 dates, a start date and end date:

             Year-Month-Day Hours:Minutes:Seconds  

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

How could I, using PHP subtract the two dates and be left with something like:

Difference: -3 days, 2 minutes and 18 seconds (If expiry date is past 3 days for example).

解决方案

This is based on numerous online examples; you'll see similar code all around if you get your google on.

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;
}

It was intended to show the difference between a given time and the current time, but I've made slight changes to show the difference between two times instead. You may wish to change the output from a postfix of ' ago' to a prefix of 'Difference: '.

这篇关于PHP时间计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆