php dateDiff 顯示兩個時間差多少天?多少分鐘?

差幾天 function

1
2
3
4
5
6
7
8
function dateDiff($startDate, $endDate) {
    $startArry = getdate(strtotime($startDate));
    $endArry = getdate(strtotime($endDate));
    $start_date = gregoriantojd($startArry["mon"], $startArry["mday"], $startArry["year"]);
    $end_date = gregoriantojd($endArry["mon"], $endArry["mday"], $endArry["year"]);
 
    return round(($end_date - $start_date), 0);
}

差幾分鐘 function

1
2
3
4
5
6
function minDiff($startTime, $endTime) {
    $start = strtotime($startTime);
    $end = strtotime($endTime);
    $timeDiff = $end - $start;
    return floor($timeDiff / 60);
}

差幾天也可以這樣寫 :

1
2
3
4
5
6
function dateDiff($startTime, $endTime) {
    $start = strtotime($startTime);
    $end = strtotime($endTime);
    $timeDiff = $end - $start;
    return floor($timeDiff / (60 * 60 * 24));
}

Comments are closed.