0

I have a user form where I let the user enter a date. I have a function that transforms the user's input into a timestamp. But the only format it can handle is mm/dd/yyyy. So, how can I transform any of these:

m/dd/yyyy, 
m/d/yyyy, 
mm/d/yyyy 
with any separator might that be 
(/,\,-,space)

into my function's accepted mask ie mm/dd/yyyy?

First I have tried to see if a string matches my description:

preg_match_all("[0-9]{0,2}/[0-9]{0,2}/[0-9]{4}", $string,$matches);

But all I get is an error

(unknown modifier "}"),

Does anyone know what's wrong ? It's my first try using a regex.

4 Answers 4

2

You're getting error because you're not using Regex delimiters.

It should be:

preg_match_all("#[0-9]{0,2}/[0-9]{0,2}/[0-9]{4}#", $string,$matches);

This of course doesn't solve your original problem but just telling you that you need to use regex in delimiters of your choice like /, #, ~, | etc.

Hint: To parse date in various formats look into strototime PHP function.

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

Comments

2

There's no need to use a regex for this (in fact it would take more work). Use PHP's built-in date/time functions instead:

strtotime: converts a string date (of practically any format) to a Unix timestamp

date: formats a timestamp into a human-readable date

$user_date;             #the user-supplied date, any format
$format = 'm/d/Y';      #your desired date format, in this case MM/DD/YYYY

#convert the date to your format
$formatted_date = date($format, strtotime($user_date));

Or, you can do this with a DateTime object. Given $user_date and $format from the above:

$user_date_obj = new DateTime($user_date);
$formatted_date = $user_date_obj->format($format);

or

$formatted_date = date_format(date_create($user_date), $format);

..

All that said, to answer your question, just delimit your regex. Slashes will work, but since you're matching literal slashes inside the regex, it's easier to use something else, say, bars/pipes:

preg_match_all("|[0-9]{0,2}/[0-9]{0,2}/[0-9]{4}|", $string, $matches);

That way, you don't need to escape your slash literals.

By the way, you can also shorten this regex to:

"|(\d{0,2}/){2}\d{4}|"

\d is the same as [0-9], and you can combine the two occurrences of \d{0,2}/ into (\d{0,2}/){2}. Some might find this harder to read, though.

Comments

1

I don't think regex is the way you want to go with this. Try using PHP's DateTime object like this:

$objDate = new DateTime($strDate);
$strNewDate = $objDate->format('m/d/Y');

Comments

1

You need to do two things:

  1. This expression needs to be formatted as: DELIMITER regex DELIMITER params

    e.g., / regex / params

  2. Escape delimiter characters if necessary

so this would be the regex for you using forward slash as delimiter:

preg_match_all("/[0-9]{0,2}\/[0-9]{0,2}\/[0-9]{4}/", $string, $matches);

4 Comments

though your solution will work, your reasoning is entirely wrong: forward slashes are not regex metacharacters, so they only need to be escaped if forward slashes are used as the regex delimiter. but you can use practically any pair of characters you want as the delimiters, even regex metacharacters, such as ||, [], {}, @@, or even aa.
Yes "/" is not the only but by far the most popular delimiter that's why I used it. It is used in other languages using PCRE. php.net/manual/en/regexp.reference.delimiters.php
i know that. your answer is still inaccurate. OP doesn't "need" to do either of the two things you said they need to do, which is why i commented.
Thanks sgroves, I have updated my answer. Do you think I have removed the problem now ?

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.