Hướng dẫn time difference between two datetime in php - chênh lệch thời gian giữa hai datetime trong php

Tôi đã thu thập nhiều chủ đề để tạo chức năng phổ quát với nhiều đầu ra (năm, tháng, ngày, giờ, phút, giây) với chuỗi hoặc phân tích phân tích cho int và hướng +- nếu bạn cần biết hướng khác biệt của phía nào.

Sử dụng ví dụ:



    $date1='2016-05-27 02:00:00';
    $format='Y-m-d H:i:s';

    echo timeDifference($date1, $format, '2017-08-30 00:01:59', $format); 
    #1 years 3 months 2 days 22 hours 1 minutes 59 seconds (string)

    echo timeDifference($date1, $format, '2017-08-30 00:01:59', $format,false, '%a days %h hours');
    #459 days 22 hours (string)

    echo timeDifference('2016-05-27 00:00:00', $format, '2017-08-30 00:01:59', $format,true, '%d days -> %H:%I:%S', true);
    #-3 days -> 00:01:59 (string)

    echo timeDifference($date1, $format, '2016-05-27 00:05:51', $format, false, 'seconds');
    #9 (string)

    echo timeDifference($date1, $format, '2016-05-27 07:00:00', $format, false, 'hours');
    #5 (string)

    echo timeDifference($date1, $format, '2016-05-27 07:00:00', $format, true, 'hours');
    #-5 (string)

    echo timeDifference($date1, $format, '2016-05-27 07:00:00', $format, true, 'hours',true);
    #-5 (int)

Hàm số



    function timeDifference($date1_pm_checked, $date1_format,$date2, $date2_format, $plus_minus=false, $return='all', $parseInt=false)
    {
        $strtotime1=strtotime($date1_pm_checked);
        $strtotime2=strtotime($date2);
        $date1 = new DateTime(date($date1_format, $strtotime1));
        $date2 = new DateTime(date($date2_format, $strtotime2));
        $interval=$date1->diff($date2);

        $plus_minus=(empty($plus_minus)) ? '' : ( ($strtotime1 > $strtotime2) ? '+' : '-'); # +/-/no_sign before value 

        switch($return)
        {
            case 'y';
            case 'year';
            case 'years';
                $elapsed = $interval->format($plus_minus.'%y');
                break;

            case 'm';
            case 'month';
            case 'months';
                $elapsed = $interval->format($plus_minus.'%m');
                break;

            case 'a';
            case 'day';
            case 'days';
                $elapsed = $interval->format($plus_minus.'%a');
                break;

            case 'd';
                $elapsed = $interval->format($plus_minus.'%d');
                break;

            case 'h';       
            case 'hour';        
            case 'hours';       
                $elapsed = $interval->format($plus_minus.'%h');
                break;

            case 'i';
            case 'minute';
            case 'minutes';
                $elapsed = $interval->format($plus_minus.'%i');
                break;

            case 's';
            case 'second';
            case 'seconds';
                $elapsed = $interval->format($plus_minus.'%s');
                break;

            case 'all':
                $parseInt=false;
                $elapsed = $plus_minus.$interval->format('%y years %m months %d days %h hours %i minutes %s seconds');
                break;

            default:
                $parseInt=false;
                $elapsed = $plus_minus.$interval->format($return);
        }

        if($parseInt)
            return (int) $elapsed;
        else
            return $elapsed;

    }

Trang chủ & nbsp; & nbsp;Cách nhận chênh lệch thời gian giữa hai lần dữ liệu trong vài phút bằng cách sử dụng PHP

Có hai cách để có được sự khác biệt về thời gian trong phút với PHP.

1. Sử dụng lớp DateTime PHP để tính toán chênh lệch giữa hai lần ngày.Phương pháp diff () của lớp DateTime tạo ra một đối tượng DateInterval tính toán chênh lệch giữa hai đối tượng ngày/thời gian theo thời gian (tổng số ngày, năm, tháng, ngày, giờ, phút, giây, v.v.). Use the PHP DateTime class to calculate the difference between two date-times. The diff() method of DateTime class creates a DateInterval object that calculates the difference between two date/time objects in time (total days, years, months, days, hours, minutes, seconds, etc.).

$datetime_1 '2022-04-10 11:15:30'; 
$datetime_2 '2022-04-12 13:30:45'; $start_datetime = new DateTime($datetime_1);
$diff $start_datetime->diff(new DateTime($datetime_2));

echo

$diff->days.' Days total
'
;
echo 
$diff->y.' Years
'
;
echo 
$diff->m.' Months
'
;
echo 
$diff->d.' Days
'
;
echo 
$diff->h.' Hours
'
;
echo 
$diff->i.' Minutes
'
;
echo 
$diff->s.' Seconds
'
;

Tính toán và nhận chênh lệch thời gian tính theo phút:

$total_minutes = ($diff->days 24 60); 
$total_minutes += ($diff->60);
$total_minutes += $diff->i;

echo

'Diff in Minutes: '.$total_minutes;

2. Bạn có thể sử dụng hàm strtotime () để có được chênh lệch thời gian giữa hai ngày (dữ liệu) trong vài phút bằng PHP. You can use strtotime() function to get the time difference between two dates (DateTimes) in minutes using PHP.

$datetime_1 '2022-04-10 11:15:30'; 
$datetime_2 '2022-04-12 13:30:45'; $from_time strtotime($datetime_1);
$to_time strtotime($datetime_2);
$diff_minutes round(abs($from_time $to_time) / 60,2). " minutes";