PHP code example of doctrineum / date-interval

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

    

doctrineum / date-interval example snippets



namespace ChopChop;

use Doctrine\ORM\Mapping as ORM;
use \Granam\DateInterval\DateInterval as GranamDateInterval;


/**
 * @ORM\Entity()
 */
class Job
{
    /**
     * @var int
     * @ORM\Id() @ORM\GeneratedValue(strategy="AUTO") @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @var GranamDateInterval
     * @ORM\Column(type="date_interval")
     */
    private $interval;

    /**
     * @return GranamDateInterval
     */
    public function getInterval(): GranamDateInterval
    {
        return $this->interval;
    }

    /**
     * @param GranamDateInterval $interval
     */
    public function setInterval(GranamDateInterval $interval)
    {
        $this->interval = $interval;
    }
}

$annualJob = new Job();
$annualJob->setInterval(new GranamDateInterval('P1Y'));

$monthlyJob = new Job();
$monthlyJob->setInterval(new GranamDateInterval('P1M'));

$dailyJob = new Job();
$dailyJob->setInterval(new GranamDateInterval('P1D'));

/** @var \Doctrine\ORM\EntityManager $entityManager */
$entityManager->persist($annualJob);
$entityManager->persist($monthlyJob);
$entityManager->persist($dailyJob);
$entityManager->flush();
$entityManager->clear();

/** @var Job[] $jobs */
$jobs = $entityManager->createQuery(
    "SELECT j FROM Jobs j WHERE j.interval < DATE_INTERVAL('P1Y') ORDER BY j.interval ASC"
)->getResult();

echo $jobs[0]->getInterval()->toSpec(); // "P1D";
echo $jobs[1]->getInterval()->toSpec(); // "P1M"
// note: to spec conversion is feature of HerreraDateInterval



use Doctrineum\DateInterval\DBAL\Types\DateIntervalType;

DateIntervalType::registerSelf();



use Doctrineum\DateInterval\ORM\Query\AST\Functions\DateIntervalFunction;
// ... $entityManager = ...
/** @var \Doctrine\ORM\EntityManager $entityManager */
DateIntervalFunction::addSelfToDQL($entityManager);