Jump to content

[SOLVED] match separator and format a date


ravi181229

Recommended Posts

You pattern is /[.|-|\/]/

Are you trying to use a character class? (if so, this won't work because you are escaping the [ and ] characters...) Is this more along the lines of what you are looking for?

 

#[./-]#

 

 

I find it easier to use delimiters other than /..../ (especially when there is going to be a / character within the patter.. just saves from having to escape it). I also direct you to have a look at how to ask regex questions. Please take note of #2 and #3 in particular (but reading them all is even better ;) )

Link to comment
Share on other sites

You can split on \D+.

 

That's an even smarter solution. Granted, in that case, couldn't you simply use \D? (unless there is some date format that I'm not thinking of that would require one [or more consecutive] non digit characters, as I'm thinking along the lines of 02/27/09, or 02-27-09, etc...).

 

In either case, your solution is even more simplified.

Link to comment
Share on other sites

I think it's best to include the + since it doesn't hurt. If the data is coming from a user they could have a typo—"02/27//2009" for instance—which, when split, would create 4 elements rather than 3. However, if there are any delimiters on the ends, this would still create empty elements. I recommend:

 

preg_split('/\D+/', $string, -1, PREG_SPLIT_NO_EMPTY)

Link to comment
Share on other sites

Thanks a lot for you help.

Your answers got my work done.

 

Final code (which works according to my expectations) :

 

    function excel_MDY2DMY($mdy) {

        $date_arr = preg_split("/(\.|-|\/)+/", $mdy);

        $dmy = (count($date_arr) == 1) ? date("Y-m-d", strtotime($date_arr[0])) : $date_arr[2]."-".$date_arr[0]."-".$date_arr[1];

        return $dmy;

    }

 

Thanks for your time.

Ravi  :)

Link to comment
Share on other sites

Well, I'm glad it works, but I am still confused why you would use "/(\.|-|\/)+/"

If you just want to extract the numbers, why not simply use Effigy's pattern instead?

 

$date_arr = preg_split('/\D+/', $mdy, -1, PREG_SPLIT_NO_EMPTY);

 

Note that alternations (...|...|...) are less efficient than character classes [...] or short-hand character classes like \D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.