This commit is contained in:
37
api/app/Utils/DateTimeUtil.php
Normal file
37
api/app/Utils/DateTimeUtil.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Utils;
|
||||
|
||||
use DateTime;
|
||||
|
||||
class DateTimeUtil
|
||||
{
|
||||
public static function isDate($string)
|
||||
{
|
||||
$string = str_replace('/', '-', $string);
|
||||
$stamp = strtotime($string);
|
||||
|
||||
if (is_numeric($stamp)) {
|
||||
|
||||
$month = date('m', $stamp);
|
||||
$day = date('d', $stamp);
|
||||
$year = date('Y', $stamp);
|
||||
|
||||
return checkdate($month, $day, $year);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function isFormattedCorrectly($date, $format = 'Y-m-d H:i:s')
|
||||
{
|
||||
if (self::isDate($date)) {
|
||||
|
||||
$createdDate = DateTime::createFromFormat($format, $date);
|
||||
|
||||
return $createdDate && $createdDate->format($format) === $date;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user