Calculate time different in minute and second

前端 未结 2 1342
花落未央
花落未央 2020-12-20 04:10

i try to compare 2 datetime and get the different in minutes and second and after i refer this topic How to get time difference in minutes in PHP yes that code can show the

相关标签:
2条回答
  • 2020-12-20 04:24

    Alternatively using the DateTime class for PHP >= 5.3:-

    $to_time = new \DateTime('2008-12-13 18:42:00');
    $from_time = new \DateTime('2008-12-13 18:41:58');
    $diff = $from_time->diff($to_time);
    echo $diff->format('%i Minutes %s Seconds');
    

    Note: `$diff' will be an instance of DateInterval.

    Or, slightly more concise, but less readable:-

    $to_time = new \DateTime('2008-12-13 18:42:00');
    $from_time = new \DateTime('2008-12-13 18:41:58');
    
    echo $to_time->diff($from_time)->format('%i Minutes %s Seconds');
    
    0 讨论(0)
  • 2020-12-20 04:38
    $minutes = round(abs($to_time - $from_time) / 60,2);
    $seconds = abs($to_time - $from_time) % 60;
    
    echo "$minutes minute, $seconds seconds";
    
    0 讨论(0)
提交回复
热议问题