1

I'm using regex to get date time from a string in proper format. if string is 2/12/2013 2 : 31 AM then i use below code to remove spaces before and after : to get 2/12/2013 2:31 AM

$dtTime = preg_replace('/(\s+\:\s+)/', ':', $dtTime);

But what to do when string is 2/12/2013 2 31 AM to get 2/12/2013 2:31 AM? I tried to use below code but not worked

$dtTime = preg_replace('/[^\/0-9](\d+\s\d+)/', ':', $dtTime)

Note that date may be separated by / or -

Purpose of above code is to get date and time separately from a string by:

    $dtime = strtotime($dtTime);
    $rtime = date('H:i:s',$dtime); // 08:50:05
    $rdate = date('Y-m-d',$dtime);
3
  • What are you going to do with this string next? Explode/extract data? Maybe this manipulations are excess then and it can be done other way? Commented May 12, 2014 at 13:53
  • after this, i'm getting date and time by $dtime = strtotime($dtTime); $rtime = date('H:i:s',$dtime); $rdate = date('Y-m-d',$dtime); Commented May 12, 2014 at 13:55
  • Where do you get such malformed datetime? Try to solve the problem at the root. Commented May 12, 2014 at 13:55

4 Answers 4

1

You can perform:

$dtTime  = preg_replace("! (\d+) (: )?(\d+) (A|P)M!", " $1:$3 $4M", $dtTime);

It will make both lines look like "2/12/2013 2:31 AM"

Sign up to request clarification or add additional context in comments.

Comments

1

If you have no control over the original printed or written format, you're just going to have to be prepared to handle a wide variety of time and date formats. Look for markers widely used for one or the other (AM/PM after some sort of time, \d{4} is a year, int > 31 must be either a year or minute or second, / and - are usually used with dates, : usually used with times, etc.) to give a clue as to what you're seeing. Don't forget that date and time could come in either order, or only one of them. If you can't plausibly match one of a number of patterns, it will just have to fail as too ambiguous input.

You're not going to do this with a single regexp!

Comments

0

I would probably approach this a different way, match the groups of numbers individually and then reformat back into a valid date, using something like /(\d+)|AM|PM/

Example here

Combined with preg_match_all(), you could do this

$pattern = '/(\d+)|AM|PM/';
$string = '2/12/2013 2 31 AM';

preg_match_all($pattern,$string,$matches);

[0] => Array
    (
        [0] => 2     // month
        [1] => 12    // day
        [2] => 2013  // year
        [3] => 2     // hour
        [4] => 31    // minutes
        [5] => AM    // ante/post meridiem
    )

From that, you can format your result into anything you want

Comments

0

You should use the native DateTime class to interpret it and reformat/clean it up instead. It'll handle parsing a variety of input better than a regex. This book by the extension's author also helps in figuring out how best to handle Dates and Times in PHP.

$dtime = new DateTime($dtTime);
$rtime = $dtime->format('H:i:s'); // 08:50:05
$rdate = $dtime->format('Y-m-d');

If you're incoming format doesn't change, use createFromFormat to make $dtime

$dtime = DateTime::createFromFormat('m/d/Y H : i A', $dtTime);

2 Comments

Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (2/12/2013 2 31 AM) at position 10 (2)
Updated with potential fix

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.