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 8210103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:57:31+00:00 2026-06-07T09:57:31+00:00

What would be the best approach to finding a date in a freeform text?

  • 0

What would be the best approach to finding a date in a freeform text? A post where a user may place a date in it in several different ways such as:

  1. July 14th & 15th
  2. 7/14 & 7/15
  3. 7-14 & 7-15
  4. Saturday 14th and Sunday 15th
  5. Saturday July 14th and 15th

and so on. Is regex my best choice for this type of thing with preg_match? I would also like to search if there are two dates, one for a start date and a second for an end date, but in the text I’m searching there may be one date or two.

This is my PHP code so far:

$dates1 = '01-01';
$dates2 = 'July 14th & 15th';
$dates3 = '7/14 & 7/15';
$dates4 = '7-14 & 7-15';
$dates5 = 'Saturday 14th and Sunday 15th';
$dates6 = 'Saturday July 14th and 15th';

$regexes = array(
        '/\s(1|2|3|4|5|6|7|8|9|10|11|12)\/\d{1,2}/',  //finds a date
        '/\s(1|2|3|4|5|6|7|8|9|10|11|12)-\d{1,2}/',  //finds another date
        '%\b(0?[1-9]|[12][0-9]|3[01])[- /.](0?[1-9]|1[012])\b%', //finds date format dd-mm or dd.mm
        );
foreach($regexes as $regex){
preg_match($regex,$dates,$matches);
}
var_dump($matches);
  • 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-06-07T09:57:33+00:00Added an answer on June 7, 2026 at 9:57 am

    PHP has a class called DateTime that manages timestamps. It lets you convert between strings and DateTime objects pretty easily… Providing that your strings use the formats PHP gives you.

    For example,

    $date = DateTime::createFromFormat('d-m', '01-01');
    $date = DateTime::createFromFormat('F d', 'July 14');
    $date = DateTime::createFromFormat('d-M-Y', '15-Feb-2009');
    

    That said, here’s what I’d do:

    Create an array of acceptable formats, in order of priority:

    $formats = array("d-m", "j-M-Y" ... );
    

    Work with RegEx to massage your inputs so that they match your formats.

    // Add the current year to this one:
    $dates1 = '01-01';
    
    // Split these into "July 14" and "July 15", and add the year
    //  (this one will be the toughest)
    $dates2 = 'July 14th & 15th';
    
    // Split these into "7/14" and "7/15", and add the year
    $dates3 = '7/14 & 7/15';
    
    // Split these into "7-14" and "7-15", and add the year
    $dates4 = '7-14 & 7-15';
    
    // Split these, and add a month and year
    $dates5 = 'Saturday 14th and Sunday 15th';
    
    // Split these, and add a year:
    $dates6 = 'Saturday July 14th and 15th';
    

    Try to construct a DateTime object:

    $date = false;
    foreach ($formats as $format)
    {
        $date = DateTime::createFromFormat($format, $dateString);
        if ($date) break;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering what would be the best approach towards finding the index of
I am wondering what would be the best approach to store, for let's say
I would like some advice on the best approach to use in the following
What would be best practice for allowing the user to store data for my
What would be the best approach to do the following: if lang==en-US: chapter=chapter if
Best would be if it performs the highlighting exactly as it is in Visual
How best would I exclude results from #other_section, given a structure like this? <form>
What would be best practice to localize your ASP.NET MVC application? I would like
What gems/technology would be best suited for creating an auction style system where a
I'm wondering what would be best to do. Right now I have running a

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.