get php DateInterval in total 'minutes'

前端 未结 5 536
鱼传尺愫
鱼传尺愫 2020-12-15 03:21

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 sampl

5条回答
  •  甜味超标
    2020-12-15 03:46

    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
    

提交回复
热议问题