PHP code example of glucnac / datetimeinterval

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))

$dateTimeInterval->getYears();
$dateTimeInterval->getHours();
$dateTimeInterval->getMinutes();
$dateTimeInterval->getSeconds();
$dateTimeInterval->getMicroseconds();

$firstDate = new DateTimeImmutable('2022-07-15');
$secondDate = new DateTimeImmutable('2022-08-15');
$dateTimeInterval = new DateTimeInterval($firstDate, $secondDate, false);

echo $dateTimeInterval->getDays(); // -31

$supDate = new DateTime('2021-08-27 00:00:00');
$infDate = new DateTime('2022-09-29 23:35:59');
$dateTimeInterval = new DateTimeInterval($firstDate, $secondDate);

// Internally uses DateInterval::format() method
echo $dateTimeInterval->format('%y years, %m months, %d days, %h hours, %i minutes, %s seconds'); // 1 years, 1 months, 2 days, 23 hours, 35 minutes, 59 seconds

$dateInterval = $dateTimeInterval->getDateInterval();

// 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