PHP code example of cocur / human-date

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

    

cocur / human-date example snippets



use Cocur\HumanDate\HumanDate;

$humanDate = new HumanDate();

echo $humanDate->transform(new DateTime('now'));
// 'Today'

echo $humanDate->transform(new DateTime('+1 day'));
// 'Tomorrow'

echo $humanDate->transform(new DateTime('-1 day'));
// 'Yesterday'

echo $humanDate->transform(new DateTime('2012-08-21'));
// 'Next Tuesday'

echo $humanDate->transform(new DateTime('2012-09-30'));
// 'September 30'

echo $humanDate->transform(new DateTime('2013-03-30'));
// 'March 30, 2013'

$translation = MyTranslation(); // must implement `Cocur\HumanDate\Translation\TranslationInterface`
$humanDate = new HumanDate($translation);

echo $humanDate->transform(new DateTime('now'));
// Calls MyTranslation::trans()

# app/AppKernel.php

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Cocur\HumanDate\Bridge\Symfony\CocurHumanDateBundle(),
        );
        // ...
    }

    // ...
}

$slug = $this->get('cocur_human_date')->slugify(new DateTime('2014-04-14'));

$slug = $this->get('human_date')->slugify(new DateTime('2014-04-14'));

use Cocur\HumanDate\Bridge\Symfony\Translation\SymfonyTranslation;
use Cocur\HumanDate\HumanDate;

// Get or create an instance of Symfony\Component\Translation\TranslatorInterface
// For example, inside a controller
$sfTrans = $this->get('translation');

// Create an adapter with translation domain "human_date" and locale "en"
// trans() passes domain and locale to every call of Symfony\Component\Translation\TranslatorInterface::trans()
// If you omit the domain and locale it uses the defaults.
$trans = new SymfonyTranslation($sfTrans, 'human_date', 'en');

$humanDate = new HumanDate($trans);

use Cocur\HumanDate\Bridge\Twig\HumanDateExtension;
use Cocur\HumanDate\HumanDate;

$twig = new Twig_Environment($loader);
$twig->addExtension(new HumanDateExtension(new HumanDate()));