Answers for "date difference in php in days"

PHP
18

php calculate date difference

//get Date diff as intervals 
$d1 = new DateTime("2018-01-10 00:00:00");
$d2 = new DateTime("2019-05-18 01:23:45");
$interval = $d1->diff($d2);
$diffInSeconds = $interval->s; //45
$diffInMinutes = $interval->i; //23
$diffInHours   = $interval->h; //8
$diffInDays    = $interval->d; //21
$diffInMonths  = $interval->m; //4
$diffInYears   = $interval->y; //1

//or get Date difference as total difference
$d1 = strtotime("2018-01-10 00:00:00");
$d2 = strtotime("2019-05-18 01:23:45");
$totalSecondsDiff = abs($d1-$d2); //42600225
$totalMinutesDiff = $totalSecondsDiff/60; //710003.75
$totalHoursDiff   = $totalSecondsDiff/60/60;//11833.39
$totalDaysDiff    = $totalSecondsDiff/60/60/24; //493.05
$totalMonthsDiff  = $totalSecondsDiff/60/60/24/30; //16.43
$totalYearsDiff   = $totalSecondsDiff/60/60/24/365; //1.35
Posted by: Guest on November-01-2019
2

Get the number of days between two dates in PHP

$startDate = new DateTime("2019-10-27");
$endDate = new DateTime("2020-04-11");

$difference = $endDate->diff($startDate);
echo $difference->format("%a");
Posted by: Guest on January-14-2021
0

difference of two dates in seconds php

$timeFirst  = strtotime('2011-05-12 18:20:20');
$timeSecond = strtotime('2011-05-13 18:20:20');
$differenceInSeconds = $timeSecond - $timeFirst;
Posted by: Guest on March-15-2021
2

php date difference in days

<?php
/**
 * array _date_diff(string)
 * function to get the difference between the date you enter and today, in days, months, or years.
 * @param string $mydate
 * @return array
 */
function _date_diff($mydate) {
    $now = time();
    $mytime = strtotime(str_replace("/", "-", $mydate)); // replace '/' with '-'; to fit with 'strtotime'
    $diff = $now - $mytime;
    $ret_diff = [
        'days' => round($diff / (60 * 60 * 24)),
        'months' => round($diff / (60 * 60 * 24 * 30)),
        'years' => round($diff / (60 * 60 * 24 * 30 * 365))
    ];
    return $ret_diff;
}

// example:
var_dump(_date_diff('2021-09-11'));
Posted by: Guest on September-24-2021

Code answers related to "date difference in php in days"

Browse Popular Code Answers by Language