src/Utilities/Date.php
public static function getDateTime($value, $allowCompoundFormats = false, $dateFormat = 'Y-m-d')
    {
        /*
         * Empty value?
         * Nothing to do :)
         */
        if (empty($value)) {
            return false;
        }

        /*
         * Instance of DateTime class?
         * Nothing to do :)
         */
        if ($value instanceof DateTime) {
            return $value;
        }

        try {
            try {
                /*
                 * Pass the value to the constructor. Maybe it's one of the allowed relative formats.
                 * Examples: "now", "last day of next month"
                 */
                $date = new DateTime($value);

                /*
                 * Instance of the DateTime class was created.
                 * Let's verify if given value is really proper date.
                 */
                $dateFromFormat = DateTime::createFromFormat($dateFormat, $value);

                if (false === $dateFromFormat) {
                    /*
                     * Nothing to do more, because:
                     * a) instance of the DateTime was created
                     * and
                     * b) if createFromFormat() method failed, given value is one of the allowed relative formats
                     * ("now", "last day of next month")
                     * and...
                     */
                    if ($allowCompoundFormats) {
                        /*
                         * ...and
                         * c) it's not an integer, e.g. not 10 or 100 or 1000
                         */
                        if (!is_numeric($value)) {
                            return $date;
                        }
                    } else {
                        $specialFormats = [
                            'now',
                        ];

                        /*
                         * ...and
                         * c) it's special compound format that contains characters that each may be used by
                         * DateTime::format() method and it raises problem while trying to verify the value at the end
                         * of this method:
                         *
                         * (new DateTime())->format($value);
                         *
                         * So, I have to refuse those special compound formats if they are not explicitly declared as
                         * compound (2nd argument of this method, set to false by default)
                         */
                        if (in_array($value, $specialFormats, true)) {
                            return false;
                        }
                    }
                } /*
                 * Verify instance of the DateTime created by constructor and by createFromFormat() method.
                 * After formatting, these dates should be the same.
                 */
                else {
                    if ($dateFromFormat->format($dateFormat) === $value) {
                        return $date;
                    }
                }
            } catch (\Exception $exception) {
                if (!$allowCompoundFormats) {
                    return false;
                }
            }

            /*
             * Does the value is a string that may be used to format date?
             * Example: "Y-m-d"
             */
            $dateString = (new DateTime())->format($value);

            if ($dateString !== (string)$value) {
                return new DateTime($dateString);
            }
        } catch (\Exception $exception) {
        }

        return false;
    }
public static function getDateTime($value, $allowCompoundFormats = false, $dateFormat = 'Y-m-d')
    {
        /*
         * Empty value?
         * Nothing to do :)
         */
        if (empty($value)) {
            return false;
        }

        /*
         * Instance of DateTime class?
         * Nothing to do :)
         */
        if ($value instanceof DateTime) {
            return $value;
        }

        try {
            try {
                /*
                 * Pass the value to the constructor. Maybe it's one of the allowed relative formats.
                 * Examples: "now", "last day of next month"
                 */
                $date = new DateTime($value);

                /*
                 * Instance of the DateTime class was created.
                 * Let's verify if given value is really proper date.
                 */
                $dateFromFormat = DateTime::createFromFormat($dateFormat, $value);

                if (false === $dateFromFormat) {
                    /*
                     * Nothing to do more, because:
                     * a) instance of the DateTime was created
                     * and
                     * b) if createFromFormat() method failed, given value is one of the allowed relative formats
                     * ("now", "last day of next month")
                     * and...
                     */
                    if ($allowCompoundFormats) {
                        /*
                         * ...and
                         * c) it's not an integer, e.g. not 10 or 100 or 1000
                         */
                        if (!is_numeric($value)) {
                            return $date;
                        }
                    } else {
                        $specialFormats = [
                            'now',
                        ];

                        /*
                         * ...and
                         * c) it's special compound format that contains characters that each may be used by
                         * DateTime::format() method and it raises problem while trying to verify the value at the end
                         * of this method:
                         *
                         * (new DateTime())->format($value);
                         *
                         * So, I have to refuse those special compound formats if they are not explicitly declared as
                         * compound (2nd argument of this method, set to false by default)
                         */
                        if (in_array($value, $specialFormats, true)) {
                            return false;
                        }
                    }
                } /*
                 * Verify instance of the DateTime created by constructor and by createFromFormat() method.
                 * After formatting, these dates should be the same.
                 */
                else {
                    if ($dateFromFormat->format($dateFormat) === $value) {
                        return $date;
                    }
                }
            } catch (\Exception $exception) {
                if (!$allowCompoundFormats) {
                    return false;
                }
            }

            /*
             * Does the value is a string that may be used to format date?
             * Example: "Y-m-d"
             */
            $dateString = (new DateTime())->format($value);

            if ($dateString !== (string)$value) {
                return new DateTime($dateString);
            }
        } catch (\Exception $exception) {
        }

        return false;
    }
public static function getDateTime ( $ value , $ allowCompoundFormats = false , $ dateFormat = 'Y-m-d' ) { /*
         * Empty value?
         * Nothing to do :)
         */ if ( empty ( $ value ) ) { return false ; } /*
         * Instance of DateTime class?
         * Nothing to do :)
         */ if ( $ value instanceof DateTime ) { return $ value ; } try { try { /*
                 * Pass the value to the constructor. Maybe it's one of the allowed relative formats.
                 * Examples: "now", "last day of next month"
                 */ $ date = new DateTime ( $ value ) ; /*
                 * Instance of the DateTime class was created.
                 * Let's verify if given value is really proper date.
                 */ $ dateFromFormat = DateTime :: createFromFormat ( $ dateFormat , $ value ) ; if ( false === $ dateFromFormat ) { /*
                     * Nothing to do more, because:
                     * a) instance of the DateTime was created
                     * and
                     * b) if createFromFormat() method failed, given value is one of the allowed relative formats
                     * ("now", "last day of next month")
                     * and...
                     */ if ( $ allowCompoundFormats ) { /*
                         * ...and
                         * c) it's not an integer, e.g. not 10 or 100 or 1000
                         */ if ( ! is_numeric ( $ value ) ) { return $ date ; } } else { $ specialFormats = [ 'now' , ] ; /*
                         * ...and
                         * c) it's special compound format that contains characters that each may be used by
                         * DateTime::format() method and it raises problem while trying to verify the value at the end
                         * of this method:
                         *
                         * (new DateTime())->format($value);
                         *
                         * So, I have to refuse those special compound formats if they are not explicitly declared as
                         * compound (2nd argument of this method, set to false by default)
                         */ if ( in_array ( $ value , $ specialFormats , true ) ) { return false ; } } } /*
                 * Verify instance of the DateTime created by constructor and by createFromFormat() method.
                 * After formatting, these dates should be the same.
                 */ else { if ( $ dateFromFormat -> format ( $ dateFormat ) === $ value ) { return $ date ; } } } catch ( \ Exception $ exception ) { if ( ! $ allowCompoundFormats ) { return false ; } } /*
             * Does the value is a string that may be used to format date?
             * Example: "Y-m-d"
             */ $ dateString = ( new DateTime ( ) ) -> format ( $ value ) ; if ( $ dateString !== ( string ) $ value ) { return new DateTime ( $ dateString ) ; } } catch ( \ Exception $ exception ) { } return false ; }
Returns the DateTime object for given value.
If the DateTime object cannot be created, false is returned.

@param mixed  $value                The value which maybe is a date
@param bool   $allowCompoundFormats (optional) If is set to true, the compound formats used to create an
instance of DateTime class are allowed (e.g. "now", "last day of next
month", "yyyy"). Otherwise - not and every incorrect value is refused
(default behaviour).
@param string $dateFormat           (optional) Format of date used to verify if given value is actually a date.
It should be format matched to the given value, e.g. "Y-m-d H:i" for
"2015-01-01 10:00" value. Default: "Y-m-d".
@return bool|DateTime
Returns the DateTime object for given value . If the DateTime object cannot be created false is returned .