PHP code example of hananils / kirby-date-methods

1. Go to this page and download the library: Download hananils/kirby-date-methods 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/ */

    

hananils / kirby-date-methods example snippets


// Get DateTime object
$datetime = $page->date()->toDateTime();

// Modify and format date
$page
  ->date()
  ->toDateTime()
  ->modify('+1 month')
  ->format('Y-m-d');

// Compare a date field to another date
$page
  ->date()
  ->toDateDiff('2000-01-01')
  ->format('The beginning of the century was %y ago.');

// Get the date formatted in the current locale style, e. g.
// '2021-01-01' becomes '1. Januar 2021' in German
echo $page->date()->toFormatted();

// Get the date formatted with a specific pattern in the current
// locale style, e. g. '2021-01-01' becomes 'Januar 2021'
echo $page->date()->toFormattedPattern('MMMM y');

// Get the relative date like '5 days ago'
echo $page->date()->toRelative();

// Get the age of a person
echo 'Nils is now ' . $page->birthday()->toAge() . ' years old';

// Given a start and an end date field, return the localized
// formatted date range, e. g. for the field values '2021-07-17'
// and '2021-07-21' return '17. – 21. Juli 2021'
echo $page->toDateRange();

// Round a date to the next full 5 minutes, e. g.
// '2021-02-01 13:42' becomes '2021-02-01 13:45'
$published = $page->published()->toDateRounded();

// This can then be formatted automatically, e. g.
// '1. Februar 2021 13:45'
$published->toFormatted(IntlDateFormatter::LONG, IntlDateFormatter::SHORT);

// Get a user's birthday this year
echo 'Bastian’s birthday is on ' .
  $user
    ->birthday()
    ->toCurrentYear()
    ->toFormatted();

// Check the given date
if ($page->published()->isEarlierThan('2010-01-01')) {
  echo 'This article is very old, please check if there are update available.'
}

$page->date()->toDateTime();

$page->date()->toDateTimeImmutable();

$page->date()->toDateInterval();

$page->date()->toDateDiff('+1 month');

// Returns 1. Januar 2020 for 2020-01-01 and de_DE
$page->date()->toFormatted();

$page->date()->toRelative('next Monday');

$page->date()->toTime();

$page->date()->toCurrentYear();

$page->date()->toCurrentMonth();

$page->date()->toCurrentDay();

// Returns 10 given '2011-08-04'
$page->date()->toAge('2021-08-04');

dateRelative('2019-12-31', 'now');

dateFormatted('de_DE', '2020-01-01');

dateRange('2020-01-01', '2020-07-01');