Php - unify date strings in different formats

I have an array with many different date formats. All lines.

I want to unify this into an array with dates (). Something like that:

function uniformize($str){
  $date = strtotime($str);
  return $date;
}

$arr = array(
  '07/29/1975',
  '20/01/1981',
  '1983-05-24',
  '10 /8 /1995'
  );

print_r( array_map('uniformize', $arr) );

      

But this only works with one format:

Array ( 
  [0] => 
  [1] => 
  [2] => 454215600 
  [3] => 
)

      

Is there a way to do this for all formats?

+3


source to share


3 answers


You can use this function for this. Fiddle here

Please note that this should always be a few days before the month. Otherwise, it is impossible to know if the first part of the date is a day or a month (if the day is more than 12 .. and cannot cover all cases).



function date2time($date)
 {
    $date = str_replace(' ','',$date);
    if(strpos($date,'-')!==false)
      $date_array = explode('-',$date);
    if(strpos($date,'/')!==false)
     $date_array = explode('/',$date);
    //add more delimiters if needed

    $day = $month = $year = '';

   foreach($date_array as $date_elem)
   {
    if(strlen($date_elem) < 3)
    {
       if(empty($day))
         $day = $date_elem;
       else
         $month = $date_elem;

    }else
      $year = $date_elem;
   }


    $time = mktime(0,0,0,$month,$day,$year);

    return $time;
  }

$arr = array(
  '07/29/1975',
  '20/01/1981',
  '1983-05-24',
  '10 /8 /1995'
  );

print_r( array_map('date2time', $arr) );

      

+1


source


Unfortunately I believe you need to define the format and then convert it to the format you want ... The
real problem is months and days ...
some use 31-12-2014

, other countries use 12-31-2014

...
so you need to determine which month and day in line, for example 05-06-2015

...

EDIT:

You have a lot of regexes ...
I am using this for the 2015-12-31 format:



if(preg_match("/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/", $yourDate, $split)) && (checkdate($split[2],$split[3],$split[1]))) {
    // do stuff where $split[1] is years, split[2] is month, and split[3] is day
}

      

Then you can adapt it to all the formats you encounter reordering ...
checkdate

checks if the date is valid (2015-52-38 is not a valid date)
You can also search some of them on Google with "date regex ", eg...

+1


source


The first two lines are used /

as a component separator. The function strtotime

considers them formatted in accordance with the American format : month/day/year

. Since 24

and 20

are invalid monthly numbers, they cannot be parsed correctly.

The last line ( '10 /8 /1995'

) looks like a short date format, but the spaces inside the components also invalidate it.

As explained in a note in the function documentationstrtotime()

:

Note:
Dates are formatted m/d/y

or d-m-y

eliminated by looking at the separator between the various components: if the separator is a forward slash ( /

) then American is assumed m/d/y

; whereas if the separator is a dash ( -

) or a period ( .

) then European format is assumed d-m-y

.

what can you do

If you know the order of the components in your dates, you can use str_replace()

to always use /

for m/d/y

(American format) and -

for d-m-y

(European format). Also, you must separate the spaces.

Another option is to write your own date parser to determine the components of the date, then use mktime()

or DateTime::setDate()

to get the date. However, if the date has day and month in 1..12

, the format cannot be detected and must be accepted (from the delimiter).

+1


source







All Articles