The script receives from different sources a string that contains the time and (or) date in different formats, perhaps there is a universal way to correctly parse this data without any bicycles, for further saving to the database in a single format? How do you solve this problem?

Here are some examples:

13.08.16 07:35 01:20 (если не указана дата то указываем текущую) 3/09/2016 07:35 3.9.16 7:01 

etc

  • one
    Write a bicycle. In PHP, for a number of years, a lot of functions for working with dates have appeared and almost all parsing functions do not take into account the features of non-English notation. Actually, there are a lot of them because of this, since they are all unsuccessful - tutankhamun
  • Try the "right parsing": 03/09/2016 and 09/03/2016 - Igor

3 answers 3

php has a built-in DateTime class for working with dates / dates and so on. which even allows you to use this syntax

$firstDayOfPastMonth->modify("0:00:00 first day of previous month");

for parsing dates, of course there is DateTime :: createFromFormat

example from the same place:

 $date = DateTime::createFromFormat('jM-Y', '15-Feb-2009'); echo $date->format('Ym-d'); 

You can use all sorts of straps based on it at your will and discretion, and you should reinvent bicycles on strtotime only when you need to parse something in the UNIX timestamp, it was created only for this.

  • Yes, a good option, and decided to use it, the only thing that complicates the work is that you need to prescribe a clear date template, instead I would like to specify only the order of the day / month / year / hour / minute after that the class would recognize in which forts recorded - user193361
  • (re-read the comment) for this you can add a binding, perhaps even not very complex. as a matter of fact, an array of formats-> pointers to handlers, or a set of cases. it is possible to abstract away even higher on the level of declarative rules - strangeqargo
  • What are the declarative rules? can be an example - user193361
  • Well, you write yaml / json / possible and a file with php-settings, in them you specify only the order of the dates and any additional settings, if you need, parse them, and then use them in the same binding from arrays of functions / cases. But it already depends on the task, if you have a project that will be used by many people, etc. If not a megaproject, then don't chase after megabitability - strangeqargo
  • Well, as an example: I once built a purely declarative interface building system for mysql json queries. You write a simple json file, get a new reporting tool in the admin panel, with all the input options, date selections, etc. This was justified, since it has been used for big and not so much money for five years, and it is used by programmers, managers and top managers - strangeqargo

PHP has a strtotime function . Everything else is from the evil one :)

  • Questionable function "... function parameter must be a string with a date in English ...". And if you need to parse "January 1, 2016" or 01/01/2016? - tutankhamun
  • one
    $str1 = '13.08.16 07:35'; $str2 = '13/08/2016 07:35'; $str3 = '13.8.16 7:35'; echo strtotime($str1).'<br/>'; echo strtotime($str2).'<br/>'; echo strtotime($str3).'<br/>'; does not give results ... white veil before eyes and everything - Alexey Shimansky
  • not parsing this function is not one of the proposed dates - user193361

Here is a good preparation: wapmorgan / TimeParser . It remains only to add the rules you need.

  • 2
    It remains only to understand, the rule of rules, by definition, is finite, and the set of options for writing dates is endless. That is, the author’s request “I want my fingers to bend over,” alas, does not give an answer. - Ipatiev