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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:46:03+00:00 2026-05-20T19:46:03+00:00

I’ve come across this in my companies code base, and I’d like to change

  • 0

I’ve come across this in my companies code base, and I’d like to change it to something that doesn’t connect to mysql just to just a function:

$sql = "select date_format(str_to_date('$date','$format'),'%Y-%m-%d') startDate ";
$result = mysql_query($sql);
...

Our production env is of the unix variety, as is my dev environment, but some people write code on a windows environment, so strptime() isn’t an option.

I’ve seen similar questions floating around SO, but none with an answer that fits my needs. Is there a simple, or common way to extract dates from strings using a variable format?

The reason the date formats can vary so much is because we’re parsing file names from vendors, so we have to be able to handle yyyymmdd, mmddyyyy, ddmmyyyy, ddmonyyyy, etc, etc.

  • 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-20T19:46:04+00:00Added an answer on May 20, 2026 at 7:46 pm

    The short answer is that there is no way to automatically parse arbitrary date formats that will correctly parse all formats out there. In your own example formats, there is just no way to know which numbers are for which date increments.

    You can look at the accepted formats of strtotime() and if all your vendors use formats recognized by it, then you are in luck. But if not, the best you can really do is create a lookup table of "vendor" => "format" and then you can use date_parse_from_format()

    edit: based on comment below, here is a php4 version of an approximation of date_parse_from_format() that should suit your needs

    function date_parse_from_format($format, $date) {
      $dMask = array('H'=>'hour','i'=>'minute','s'=>'second','y'=>'year','m'=>'month','d'=>'day');
      $format = preg_split('//', $format, -1, PREG_SPLIT_NO_EMPTY);  
      $date = preg_split('//', $date, -1, PREG_SPLIT_NO_EMPTY);  
      foreach ($date as $k => $v) {
        if ($dMask[$format[$k]]) $dt[$dMask[$format[$k]]] .= $v;
      }
      return $dt;
    }
    

    Basically you need to have a lookup table of vendor => format where format is a mask of what each character in the date string represents.

    NOTE: The masks used for each date/time increments do NOT exactly reflect what is used in php’s normal date() string format. It is simplified and meant to mask each individual character of your custom string.

    Example:

    /*
      lookup table for vendors
      for EVERY character you want to count as a date/time 
      increment you must use the following masks:
      hour   : H
      minute : i
      second : s
      year   : y
      month  : m
      day    : d
    */
    $vendorDateFormats = array(
      'vendor1' => 'yyyymmdd',
      'vendor2' => 'mmddyyyy',
      'vendor3' => 'ddmmyyyy',
      'vendor4' => 'yyyy.mm.dd HH:ii:ss'
    );
    
    // example 1:
    echo "<pre>";
    print_r(date_parse_from_format($vendorDateFormats['vendor2'],'03232011'));
    
    // example 2:
    echo "<pre>";
    print_r(date_parse_from_format($vendorDateFormats['vendor4'],'2011.03.23 12:03:00'));
    

    output:

    Array
    (
        [month] => 03
        [day] => 23
        [year] => 2011
    )
    
    Array
    (
        [year] => 2011
        [month] => 03
        [day] => 23
        [hour] => 12
        [minute] => 03
        [second] => 00
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.