Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7177965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:49:25+00:00 2026-05-28T16:49:25+00:00

I would like to create a function which validates a date string. My module

  • 0

I would like to create a function which validates a date string.

My module displays a date, and in the backend there are some example formats the user can use to display the date on the frontend, and also an advanced date format input where he can specify a date format string like ‘H:i:s’ or whatever he wants.

If no advanced format option is specified, it uses the default format.

To check the validity of the enterred format in the input, I thought I can do :

$now = new Datetime(‘now’);

if (!$now->format($format)) …. false format

But the format method doesn’t return false if $format = x or efkjfkefefje, it just returns the string itself.

I would like it to return false if there is a caracter that differs from the specified caracters here : http://www.php.net/manual/en/function.date.php

But there are so many cases. For example ‘H:i:s’ would be valid but ‘His’ Won’t be.

I don’t want it to parse the ‘:’ or ‘/’, I mean we can consider that ‘H/i/s’ is valid, because it’s an input for advanced users only.

I would like it to return false if a caracter differs from the list here http://www.php.net/manual/en/function.date.php or if there is no space between caracters : ‘His’ is not valid whereas the 3 caracters alone are.

I’m not very familiar with regex.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T16:49:26+00:00Added an answer on May 28, 2026 at 4:49 pm

    To validate a date format string, you should be able to use DateTime::createFromFormat.

    http://www.php.net/manual/en/datetime.createfromformat.php

    It returns either a DateTime object if it’s created successfully, or false if the date string provided does not match the format.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to create some sort of function where you pass in a
I would like to create a function which can take two arguments object and
I would like to create a debugging helper function which I can use within
I would like to create a JavaScript function which returns the value of a
i would like to create a function which accepts a constant like the datepart
I would like to create a test function which change the '.' charachter into
I would like to create a small function, which should show the current status
There is a member function which I would like to delete the current object
I would like to create a function that will return list of type that
I would like to create a function that checks if a numeric value passed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.