for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types=1);
/*
* This file is part of Cecil.
*
* Copyright (c) Arnaud Ligny <[email protected]>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cecil\Util;
class Date
{
/**
* Checks if a date is valid.
public static function isValid(string $date, string $format = 'Y-m-d'): bool
$d = \DateTime::createFromFormat($format, $date);
return $d && $d->format($format) === $date;
}
* Date to DateTime.
* @param mixed $date Can be a DateTIme object, a timestamp or a string
public static function toDatetime($date): \DateTime
// DateTime
if ($date instanceof \DateTime) {
return $date;
// timestamp or 'AAAA-MM-DD'
if (is_numeric($date)) {
return (new \DateTime())->setTimestamp($date);
// string (ie: '01/01/2019', 'today')
return new \DateTime($date);