1. Go to this page and download the library: Download glucnac/datetimeinterval library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
glucnac / datetimeinterval example snippets
$firstDate = new DateTimeImmutable('2022-07-15');
$secondDate = new DateTimeImmutable('2022-08-15');
$dateTimeInterval = new DateTimeInterval($firstDate, $secondDate);
// Absolute count of days
echo $dateTimeInterval->getDays(); // 31
// Relative count of days
echo $dateTimeInterval->getDays(false); // 0 (15th day of the month (august) - 15th day of the month (july))
$firstDate = new DateTimeImmutable('2022-07-15');
$secondDate = new DateTimeImmutable('2023-08-15');
$dateTimeInterval = new DateTimeInterval($firstDate, $secondDate);
// Absolute count of months
echo $dateTimeInterval->getMonths(); // 13
// Relative count of months
echo $dateTimeInterval->getMonths(false); // 1 (8th month of the year (august) - 7th month of the year (july))
// Let's say you want to compute some delays between 2022-07-15 and 2022-08-15.
$firstDate = new DateTimeImmutable('2022-07-15');
$secondDate = new DateTimeImmutable('2022-08-15');
$dateInterval = date_diff($secondDate, $firstDate);
// OR
$dateInterval = $firstDate->diff($secondDate);
// You want to count the days between these two dates. Using DateInterval,
// you notice that two properties are available :
// PhpDoc states: "Totals number of days in the interval span"
$dateInterval->days;
// PhpDoc states: "Number of days"
$dateInterval->d;
// Well, the doc of ->d is not really clear, let's see the difference between these two returns
echo $dateInterval->days; // 30
echo $dateInterval->d; // 0
// Nice, it seems ->d is a relative count, whereas ->days give an absolute count
// What about getting the number of months then ?
// PhpDoc states: "Number of months"
echo $dateInterval->m // 1
// We don't yet if it's a relative or an absolute count. So let's change the year to next one :
$firstDate = new DateTimeImmutable('2022-07-15');
$secondDate = new DateTimeImmutable('2023-08-15');
$dateInterval = date_diff($secondDate, $firstDate);
echo $dateInterval->m // 1
// Now it's clear: ->m also returns a relative count
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.