-1

I have a file that has a set of timestamps and I look for a way to filter them and find the wrong formatted from the well formatted by using a regex expression :

$line = array(20171203124500,20150120183045,20140621064431);
$args = array('filter' => FILTER_VALIDATE_REGEXP,
              'options' => array('regexp' => 'REGEX-HERE'), );
$result = filter_var_array($line, $args);
var_dump($result);

I also want to check if the day of the month is correct :

  • the date 31 April should not match
  • the date 29/02/2003 should not match and 29/02/2004 should.

I have this expression :

^(?:(?:(?:0?[1-9]|1\d|2[0-8])\/(?:0?[1-9]|1[0-2]))\/(?:(?:1[6-9]|[2-9]\d)\d{2}))$|^(?:(?:(?:31\/0?[13578]|1[02])|(?:(?:29|30)\/(?:0?[1,3-9]|1[0-2])))\/(?:(?:1[6-9]|[2-9]\d)\d{2}))$|^(?:29\/0?2\/(?:(?:(?:1[6-9]|[2-9]\d)(?:0[48]|[2468][048]|[13579][26]))))$

But it is given for dates with this format : 31/01/2001

This expression validates dates in the Brazilian d/m/y format (from 1/1/1600 - 12/31/9999). The days are validated for the given month and year. Leap years are validated for all 4 digits years from 1600-9999. Days and months must be 1 or 2 digits and may have leading zeros. Years must be 4 digit years, between 1600 and 9999. Date separator must be a slash (/)

How to adapt this regexp to match the format I look for : YYYYmmddHHMMSS

JavaQueen
  • 1,155
  • 2
  • 19
  • 44
  • Just exchange day and year part. – Toto Feb 09 '17 at 09:13
  • 1
    @Toto the question is not duplicate, the one you mentioned is not at all what I'm looking for – JavaQueen Feb 09 '17 at 09:24
  • 1
    @JavaQueen: The regex is taken from [my old Java answer](http://stackoverflow.com/a/31133075/3832970) without any reference to that. I would not give any rep points to Rarasen for that. I would delete this post altogether. – Wiktor Stribiżew Feb 09 '17 at 09:58

1 Answers1

1

The regex should be :

(?:(?:(?:(?:(?:[13579][26]|[2468][048])00)|(?:[0-9]{2}(?:(?:[13579][26])|(?:[2468][048]|0[48]))))(?:(?:(?:09|04|06|11)(?:0[1-9]|1[0-9]|2[0-9]|30))|(?:(?:01|03|05|07|08|10|12)(?:0[1-9]|1[0-9]|2[0-9]|3[01]))|(?:02(?:0[1-9]|1[0-9]|2[0-9]))))|(?:[0-9]{4}(?:(?:(?:09|04|06|11)(?:0[1-9]|1[0-9]|2[0-9]|30))|(?:(?:01|03|05|07|08|10|12)(?:0[1-9]|1[0-9]|2[0-9]|3[01]))|(?:02(?:[01][0-9]|2[0-8])))))(?:0[0-9]|1[0-9]|2[0-3])(?:[0-5][0-9]){2}

To know if a date is valid, you need to use checkdate($month, $day, $year).

See doc: checkdate

Toto
  • 89,455
  • 62
  • 89
  • 125
Rarasen
  • 64
  • 3