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

  • Home
  • SEARCH
  • 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 6939917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:41:11+00:00 2026-05-27T12:41:11+00:00

I need to extract the date out of a string variable, and the date

  • 0

I need to extract the date out of a string variable, and the date are formatted in various kind of formats as below:

$date1 = "03/12/2011 (Sat)";
$date2 = "3.12.2011 SAT";
$date3 = "Date: 03/12/2011 "; /* <-- the extra trailing space is intentional */
$date4 = "date:03/12/2011";
$date5 = "date: 03/12/2011";
$date6 = "03/12/2011";
$date7 = "13.12.2011 TUE";

What is the best way to create a PHP function which will work for all the input variables above in extracting the correct date info?

  • 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-27T12:41:12+00:00Added an answer on May 27, 2026 at 12:41 pm

    For more info on the DateTime object returned by the function, check the PHP documentation for the DateTime class.

    /**
     * Parses date from string
     * @param string $str Uncorrected date string
     * @return DateTime PHP datetime object
     */
    function date_grab($str)
    {
      // regex pattern will match any date formatted dd-mm-yyy or d-mm-yyyy with
      // separators: periods, slahes, dashes
      $p = '{.*?(\d\d?)[\\/\.\-]([\d]{2})[\\/\.\-]([\d]{4}).*}';
      $date = preg_replace($p, '$3-$2-$1', $str);
      return new \DateTime($date);
    }
    
    // verify that it works correctly for your values:
    $arr = array(
      "03/12/2011 (Sat)",
      "3.12.2011 SAT",
      "Date: 03/12/2011 ", /* <-- the extra trailing space is intentional */
      "date:03/12/2011",
      "date: 03/12/2011",
      "03/12/2011"
    );
    
    foreach ($arr as $str) {
      $date = date_grab($str);
      echo $date->format('Y-m-d') . "\n";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to extract the last date only from a string that looks like
I have string of date from xml file of such kind: '2010-09-09T22:33:44.OZ' I need
So I need to set a DATE variable equal to tomorrow's date and I
I need to use python to extract the date from filenames. The date is
I use this to extract all string literals that need translation: xgettext -o $@
I have a java function to extract a string out of the HTML Page
I need to extract and decode barcodes and text from images. Is there any
I need to extract all the emails that show up as autocomplete entries in
I need to extract data from a .mpp file on the network and combine
I need to extract multiple text/dropdown list fields from an asp.net form and format

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.