Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中获取日期/时间差_Php_Date_Time - Fatal编程技术网

在PHP中获取日期/时间差

在PHP中获取日期/时间差,php,date,time,Php,Date,Time,我想知道两天之间的时差。但对于某些日期/时间,我会得到错误的答案 这是我的密码: /**************************************** $start_date = new DateTime('23:58:40'); *These two still give $end_date = new DateTime('00:00:00'); *a wrong answer *****************************************/ $sta

我想知道两天之间的时差。但对于某些日期/时间,我会得到错误的答案

这是我的密码:

/****************************************
$start_date = new DateTime('23:58:40'); *These two still give 
$end_date = new DateTime('00:00:00');   *a wrong answer
*****************************************/

$start_date = new DateTime('23:58:40');
$end_date = new DateTime('00:11:36');

$dd = date_diff($end_date, $start_date);

//Giving a wrong answer: Hours = 23, Minutes = 47, Seconds = 4 
echo "Hours = $dd->h, Minutes = $dd->i, Seconds = $dd->s";

将参数交换为date\u diff

$dd = date_diff($start_date, $end_date);
编辑


在实际测试了这个理论之后,它被证明是完全无用的,给出了同样的答案

将参数交换为date\u diff

$dd = date_diff($start_date, $end_date);
编辑


在实际测试了这个理论之后,它被证明是完全无用的,给出了同样的答案

雨篷是正确的。你提供两次。没有日期就无法知道最后一个日期实际上是第二天。仅仅因为您将变量命名为“end_date”,并不意味着PHP知道您的意思

也许你应该在你的请求中包括日期,比如

$start_date = new DateTime('2012-12-07 23:58:40');
$end_date = new DateTime('2012-12-08 00:11:36');
如果你真的想和时代一起工作:

function differenceInTimes($start, $end) {
    if (strtotime($start)>strtotime($end)) {
        //start date is later then end date
        //end date is next day
        $s = new DateTime('2000-01-01 '.$start);
        $e = new DateTime('2000-01-02 '.$end);
    } else {
        //start date is earlier then end date
        //same day
        $s = new DateTime('2000-01-01 '.$start);
        $e = new DateTime('2000-01-01 '.$end);
    }

    return date_diff($s, $e);
}

$start_date = '23:58:40';
$end_date = '00:11:36';
$dd = differenceInTimes($start_date, $end_date);
echo "Hours = $dd->h, Minutes = $dd->i, Seconds = $dd->s";

//Hours = 0, Minutes = 12, Seconds = 56 

答案是正确的。你提供两次。没有日期就无法知道最后一个日期实际上是第二天。仅仅因为您将变量命名为“end_date”,并不意味着PHP知道您的意思

也许你应该在你的请求中包括日期,比如

$start_date = new DateTime('2012-12-07 23:58:40');
$end_date = new DateTime('2012-12-08 00:11:36');
如果你真的想和时代一起工作:

function differenceInTimes($start, $end) {
    if (strtotime($start)>strtotime($end)) {
        //start date is later then end date
        //end date is next day
        $s = new DateTime('2000-01-01 '.$start);
        $e = new DateTime('2000-01-02 '.$end);
    } else {
        //start date is earlier then end date
        //same day
        $s = new DateTime('2000-01-01 '.$start);
        $e = new DateTime('2000-01-01 '.$end);
    }

    return date_diff($s, $e);
}

$start_date = '23:58:40';
$end_date = '00:11:36';
$dd = differenceInTimes($start_date, $end_date);
echo "Hours = $dd->h, Minutes = $dd->i, Seconds = $dd->s";

//Hours = 0, Minutes = 12, Seconds = 56 

为什么这是错误的答案?同一天早上0点到晚上23点之间有23个小时的差异。结果是正确的。我觉得你需要交换参数才能得到你想要的结果?@eskimo:交换参数只会影响结果
DateInterval
对象的
inversed
属性。@eskimo我按照你的思路思考,但显然我的答案是错误的:)你期望的结果是什么?为什么这是错误的答案?同一天早上0点到晚上23点之间有23个小时的差异。结果是正确的。我觉得你需要交换参数才能得到你想要的结果?@eskimo:交换参数只会影响结果
DateInterval
对象的
inversed
属性。@eskimo我也这么想,但显然我的答案是错的:)你期望的结果是什么?好的,我也很困惑。好吧,我也很困惑。