Hướng dẫn php dateinterval total minutes

I am trying to get the PHP "DateInterval" value in "total minutes" value. How to get it? Seems like simple format["%i minutes"] not working?

Here is the sample code:

$test = new \DateTime["48 hours"];
$interval = $test->diff[new \DateTime[]];

Now if I try to get the interval in total days, its fine:

echo $interval->format['%a total days'];

It is showing 2 days as output, which is totally fine. What I am trying to get if to get the value in "total minutes", so I tried:

echo $interval->format['%i total minutes'];

Which is not working. Any help appreciated to get my desired output.

asked May 27, 2013 at 15:14

RanaRana

5,77211 gold badges55 silver badges91 bronze badges

8

abs[[new \DateTime["48 hours"]]->getTimestamp[] - [new \DateTime]->getTimestamp[]] / 60

That's the easiest way to get the difference in minutes between two DateTime instances.

answered May 27, 2013 at 15:35

decezedeceze

497k81 gold badges718 silver badges865 bronze badges

7

If you are stuck in a position where all you have is the DateInterval, and you [like me] discover that there seems to be no way to get the total minutes, seconds or whatever of the interval, the solution is to create a DateTime at zero time, add the interval to it, and then get the resulting timestamp:

$timeInterval      = //the DateInterval you have;
$intervalInSeconds = [new DateTime[]]->setTimeStamp[0]->add[$timeInterval]->getTimeStamp[];
$intervalInMinutes = $intervalInSeconds/60; // and so on

answered May 15, 2014 at 10:12

Neil TownsendNeil Townsend

5,9545 gold badges34 silver badges50 bronze badges

0

I wrote two functions that just calculates the totalTime from a DateInterval. Accuracy can be increased by considering years and months.

function getTotalMinutes[DateInterval $int]{
    return [$int->d * 24 * 60] + [$int->h * 60] + $int->i;
}

function getTotalHours[DateInterval $int]{
    return [$int->d * 24] + $int->h + $int->i / 60;
}

answered Feb 22, 2016 at 9:58

GenmaisGenmais

1,0491 gold badge9 silver badges14 bronze badges

1

That works perfectly.

function calculateMinutes[DateInterval $int]{
    $days = $int->format['%a'];
    return [$days * 24 * 60] + [$int->h * 60] + $int->i;
}

answered Nov 10, 2016 at 12:13

0

Here is the excepted answer as a method in PHP7.2 style:

/**
 * @param \DateTime $a
 * @param \DateTime $b
 * @return int
 */
public static function getMinutesDifference[\DateTime $a, \DateTime $b]: int
{
    return abs[$a->getTimestamp[] - $b->getTimestamp[]] / 60;
}

answered Jun 21, 2019 at 8:26

This question is about minutes but if you want to recalculate every carry over points [like I needed to] you can use this solution suggested by @glavic in the comments on the php.net man page [simplified and turned into a function]:

private function calculateCarryOverPoints[\DateInterval $dateInterval]: \DateInterval
{
    $from = new \DateTime;
    $to = clone $from;
    // Add time of dateInterval to empty DateTime object
    $to = $to->add[$dateInterval];
    // Calculate difference between zero DateTime and DateTime with added DateInterval time
    // Which returns a DateInterval object $diff with correct carry over points [days, hours, minutes, seconds etc.]
    return $from->diff[$to];
}

answered May 26 at 15:22

Chủ Đề