PHP code example of romeoz / rock-date

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

    

romeoz / rock-date example snippets


use rock\date\DateTime;

(new DateTime)->format(); // output: current date in the format Y-m-d H:i:s

// default format 
(new DateTime)->isoDate(); // output: current date in the format Y-m-d

// modify date
DateTime::set('1988-11-12')->date(); //output: 11/12/1988

$datetime = DateTime::set('1988-11-12');
$datetime->setFormats(['shortDate' => 'j / F / Y']);

$datetime->shortDate(); // output: 12 / November / 1988

$datetime = new DateTime('1988-11-12');
$datetime->setFormatOption('ago', function (DateTime $datetime) {
    return floor((time() - $datetime->getTimestamp()) / 86400) . ' days ago';
});

$datetime->format('d F Y, ago'); // output: 12 November 1988, 9574 days ago

$dateTime = new DateTime('1988-11-12');
$dateTime->setLocale('ru');

$dateTime->format('j  F  Y'); // output: 12  ноября  1988 

$diff = (new DateTime('2012-02-01'))->diff(new DateTime('2015-01-01'));

echo $diff->total_months; // output: 36
echo $diff->format('%R%tm months'); // output: +36 months