note 126477 added to function.checkdate

From: Date: Tue, 05 Oct 2021 14:03:42 +0000
Subject: note 126477 added to function.checkdate
Groups: php.notes 
Request: Send a blank email to php-notes+get-217030@lists.php.net to get a copy of this message
I use a slight variation of the validateDate() above, to also allow validating dates like: '2021-10-05T13:55:01Z' with DateTime::ATOM like: <?php function validateDate($date, $format = 'Y-m-d H:i:s') { // replace a 'Z' at the end by '+00:00' $date = preg_replace('/(.*)Z$/', '${1}+00:00', $date); $d = DateTime::createFromFormat($format, $date); return $d && $d->format($format) == $date; } ?> This means a call like: <?php var_dump(validateDate('2021-10-05T13:55:01Z', DateTime::ATOM)); ?> will output true I use this in a filter_input wrapper like: <?php function fi_validateDate($format) { return function($value = null) use ($format) { return validateDate($value, $format) ? $value : null; }; }?> so I can do stuff like: <?php $dateFrom = filter_input(INPUT_GET, 'dateFrom', FILTER_CALLBACK, ['options' => fi_validateDate(DateTime::ATOM)]); ?> ---- Server IP: 45.112.84.4 Probable Submitter: 45.112.85.242 (proxied: 84.26.112.131) ---- Manual Page -- https://php.net/manual/en/function.checkdate.php Edit -- https://main.php.net/note/edit/126477 Del: integrated -- https://main.php.net/note/delete/126477/integrated Del: useless -- https://main.php.net/note/delete/126477/useless Del: bad code -- https://main.php.net/note/delete/126477/bad+code Del: spam -- https://main.php.net/note/delete/126477/spam Del: non-english -- https://main.php.net/note/delete/126477/non-english Del: in docs -- https://main.php.net/note/delete/126477/in+docs Del: other reasons-- https://main.php.net/note/delete/126477 Reject -- https://main.php.net/note/reject/126477 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#217030) next »