StreetCommunityProgrammer / metaphore

Story as Code. Public Collections of Metaphore our Freestyler accross the world. Gain knowledge with unusual perspective from our Punk members.
https://metaphore.vercel.app
GNU General Public License v3.0
26 stars 5 forks source link

Check a String is a Valid Date or DateTime format in PHP #68

Closed darkterminal closed 1 year ago

darkterminal commented 1 year ago

The Back Story about your PHP Metaphor

I writing a story helper (little helper) to validate schema using PHP in my open-source project called SleekwareDB a Flat File database and RESTfull API using SleekDB. I know we have many libraries out there, never mind. It's just freestyled in my old language.

The PHP Story!

You can check if a string is a valid date or datetime format in PHP using the DateTime::createFromFormat() method. This method returns a DateTime object if the input string is a valid date/datetime in the specified format, or false if it is not valid.

Here's an example function that checks whether a string is a valid date or datetime in the given format:

function isDateTime($dateString, $format) {
    $dateObj = DateTime::createFromFormat($format, $dateString);
    return $dateObj !== false && !array_sum($dateObj->getLastErrors());
}

You can use this function like this:

$dateString1 = '2022-05-01';
$dateString2 = '2022-05-01 10:30:00';
$format1 = 'Y-m-d';
$format2 = 'Y-m-d H:i:s';

if (isDateTime($dateString1, $format1)) {
    echo "$dateString1 is a valid date format.";
} else {
    echo "$dateString1 is not a valid date format.";
}

if (isDateTime($dateString2, $format2)) {
    echo "$dateString2 is a valid datetime format.";
} else {
    echo "$dateString2 is not a valid datetime format.";
}

This code will output:

2022-05-01 is a valid date format.
2022-05-01 10:30:00 is a valid datetime format.

That's it! Small and cringe... :smile:

A PHP demo/repos link

No response

PayPal Link for Donation (PHP Storyteller)

No response

github-actions[bot] commented 1 year ago

Hello Punk! It's great having you contribute to this project

Welcome to the community :neckbeard:

If you would like to continue contributing to open source and would like to do it with an awesome inclusive community, you should join our GitHub Organisation - we help and encourage each other to contribute to open source little and often :neckbeard:. Any questions let us know.