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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:41:18+00:00 2026-05-26T17:41:18+00:00

I have a collection of documents and I’m trying to pull the dates out

  • 0

I have a collection of documents and I’m trying to pull the dates out of them. They are plain text and HTML mostly but the date formats they use very greatly (though they are all English dates). How can I find and parse dates like this in a long string of text?

updated 2011-03-21T00:43:14
Sunday, March 20, 2011
Wednesday, March 16, 2011 | 11:25 AM
March 20, 2011 @ 12:21 pm
May 5, 2011
Published March 19, 2011
Some text here (March 19, 2011)
10/28/2011 21:16
<a href="#>Author Name</a> on Mar 17th 2011 ...
Location, ABBR., Jan. 8, 2008
01/07/2008 (6:00 pm)
By Author Name and Company 03/19/2011 09:59
Posted by Author Name on March 16, 2011 at 03:20 PM EDT
  • 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-26T17:41:19+00:00Added an answer on May 26, 2026 at 5:41 pm

    Have a look at the strtotime function.

    // Output: March 20th, 2011 12:00:00 AM
    echo date( 'F jS, Y h:i:s A', strtotime( "Sunday, March 20, 2011"));
    

    Edit: Here is a more complete example showing how to parse a bunch of the dates provided.

    <?php
    $dates = array( '03/19/2011 09:59', 'Wednesday, March 16, 2011 | 11:25 AM', 'Sunday, March 20, 2011', 'March 20, 2011 @ 12:21 pm', 'May 5, 2011');
    foreach( $dates as $date)
    {
        echo $date . ' ---- ' . date( 'F jS, Y h:i:s A', strtotime( str_replace( array( '@', '|'), '', $date))) . "<br />\n";
    }
    

    Demo

    Of course, some dates will not parse as-is since they are not supported by the list of date formats – For those, you’ll need to do some additional filtering / parsing to either extract their date or form them into a string suitable for strtotime.

    Edit: Since there’s an interest in further processing of the input string, here is an example of how you can parse the text without using a regex to get the dates out. Notice how some of the dates just can’t be extracted, for this you will either need more string processing, or to use a regex.

    As a side note, I would investigate using a regex if the provided string is only one of many variants of lines that contain dates. However, if the provided string is the only formats that the dates will be found in, string processing should be enough.

    $str = 'updated 2011-03-21T00:43:14
    Sunday, March 20, 2011
    Wednesday, March 16, 2011 | 11:25 AM
    March 20, 2011 @ 12:21 pm
    May 5, 2011
    Published March 19, 2011
    Some text here (March 19, 2011)
    10/28/2011 21:16
    <a href="#">Author Name</a> on Mar 17th 2011 ...
    Location, ABBR., Jan. 8, 2008
    01/07/2008 (6:00 pm)
    By Author Name and Company 03/19/2011 09:59
    Posted by Author Name on March 16, 2011 at 03:20 PM EDT';
    
    foreach( explode( "\n", $str) as $line)
    {
        $line = str_replace( array( '@', '|', '(', ')'), '', trim( $line));
        $line = strip_tags( $line);
        if( ($time = strtotime( $line)) === false)
        {
            echo "Could not parse line - '" . $line . "'\n"; // Need additional processing / regex here
            continue;
        }
        echo "Converted '" . $line . "' to '" . date( 'F jS, Y h:i:s A', $time) . "'\n";
    }
    

    Demo.

    Final Edit:

    Finally, an example how to do some text processing to get more of the dates to parse.

    foreach( explode( "\n", $str) as $line)
    {
        $line = str_replace( array( '@', '|', '(', ')', 'Published', '...'), '', trim( $line));
        $line = strip_tags( trim( $line));
        if( ($time = strtotime( $line)) === false)
        {
            if( !(($on_position = stripos( $line, 'on')) === false))
            {
                $line = substr( $line, $on_position + 3);
                if( ($time = strtotime( trim( $line))) === false)
                {
                    echo "Could not parse line that contains 'on' - '" . $line . "'\n";
                    continue;
                }
            }
            echo "Could not parse line - '" . $line . "'\n";
            continue;
        }
        echo "Converted '" . $line . "' to '" . date( 'F jS, Y h:i:s A', $time) . "'\n";
    }
    

    Demo

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

Sidebar

Related Questions

I have a collection of documents that I'm attempting to parse. Like HTML, they
For example, I have collection with documents, where documents can have field url (but
I have a collection of HTML documents for which I need to parse the
The problem is: I have a collection of text documents, i want to pick
Lets say I have collection of documents with specified longitude and latitude. type is
for example I have collection foo of documents like that: {tag_cloud:[{value:games, count:10}, {value:girls, count:500}]}
I have a collection with nested documents in it. Each document also has an
I have a mongo collection which has documents with two fields fieldA and fieldB,
I have a collection with 1.5 million documents. I'm counting using PHP: $db->some->ensureIndex(array(sometext =>
I have an existing mongo database where all documents in a collection have 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.