How to validate a valid month / year between a range - php

How can I validate the date input from users following the format mm/yyyy and also the yyyy range is from 2002 until 2030. So the month should be between 1 to 12 and all of the years below 2002 and 2030 in invalid. Here is my current regular expression:
^(0[1-9]|1[012])[- /.](20)\d\d$

Since you're forced to use regex, here you are:
^(0[1-9]|1[012])[- /.](20(?:0[2-9]|[12]\d|30))$
It works similar to the month validation pattern you wrote - 0[2-9] matches 2002 to 2009, [12]\d matches 2010 to 2029, and 30 matches 2030.

You can Validate a Gregorian date using the checkdate() function; however this requires you to be in a DateTime/Date format, it's useful for making sure people don't do stuff like: 35/02/2014
However, something like this could work?
([0-2][0-9]|31)\/(0[1-9]|1[0-2])\/(200[2-9]|20[1-2][0-9]|2030)
And then cast the string to a DateTime object and run the checkdate() function upon it to check it's gregorian?

^(0[1-9]|(1[0-2]+)+)\/(20(0[2-9]|([1-2][0-9])|30))$
You can use this pattern. You can test it here also. http://regexr.com/3a60s
Breakdown:
^(
0[1-9]|(1[0-2]+)+)
\/
(20(0[2-9]|([1-2][0-9])|30)
)$
Explanation:
Matches between 01 to 09, or 10 to 12.
Matches your "/"
Matches 3 year sets starting with 20.
2002 to 2009 with 0[2-9]
or 2010 to 2029 with [1-2][0-9]
or 2030, which is matched with the literal 30

You may check by pattern.. this way
<input type="text" pattern="^(0[1-9]|1[012])[- /.](20(?:0[2-9]|[12]\d|30))$" />

Related

PHP Regex to match Javascript date output

I am trying to find a PHP regex to find Javascript date output in JSON strings. I'll read all JSON objects from a database and convert each string into DD/MM/YYYY format.
For example Tue Sep 24 2019 14:12:51 GMT+0000 (Greenwich Mean Time)
I almost made it, probably it is not any good but could not finish it as well :(
[a-zA-Z]{3} [a-zA-Z]{3} \d{2} \d{4} \d{2}:\d{2}:\d{2} GMT\D\d{4}
The date might also be Tue Sep 24 2019 14:12:51 GMT+0100 (British Summer Time) so I need to find anything in parentheses as well.
The JSON object can have multiple dates inside it so I'll need to find and replace all of them.
Thank you!
((?:[a-zA-Z]{3} ){2}\d{2} \d{4} \d{2}:\d{2}:\d{2} GMT[+-]\d{4}) \(([^\)\n]+)\)
This regex will select the entire string, and will also separately capture both the date string and whatever is in the parenthesis for you to process on your own.
Try it here!

How to preg_replace same items in one subject php?

I have a question regarding one case.
I'm using php preg_replace() function to format data from this string:
12:05 Place1 12:40 14:00 16:30 Place2 "Test" 29 Janury
I need it to be outputed like this:
<li>29 January - Place1 - 12:05</li>
<li>29 January - Place2 "Test" - 12:40 14:00 16:30</li>
My regular expression:
/(\d[0-9]:\d[0-9]).+?(\D+).+?(\d[0-9]\s(January|February|March))/
I'm currently using something like this:
$text = "12:05 Place1 12:40 14:00 16:30 Place2 "Test" 29 Janury";
$data = preg_replace("/(\d[0-9]:\d[0-9]).+?(\D+).+?(\d[0-9]\s(January|February|March))/", "<li>$3 - $2 - $1</li>", $text);
echo $data;
The problem it shows only first matches,
29 January - Place1 - 12:05
Maybe someone know how to solve this case?
Thanks :)
It is not possible to do in one regex because you want to use a match from the end of the string before the PCRE engine has reached it. You need to do this in two steps:
preg_match() with PREG_OFFSET_CAPTURE to capture the date from the end of the string into another variable. Use the offset it provides to truncate the original string.
preg_match_all() the truncated string to get the times & places as an array, then iterate the array to use each match & the date variable to create the list. '/((?:\s*+\d\d:\d\d)++)\s++((?:.(?!\d\d:\d\d\s))++)\s*+/' is a suitable pattern.

PHP: preg replace date time

how can i replace date/time in this format 'Fri Mar 23 15:21:08 2012' with preg_replace?
Date in this format is present couple of times in my text and i need to replace it with current time/date.
Thanks,
Chris
Well, what you need is an expression that will match 3 letters (Fri) followed by a space and another three letters (Mar).
First we need to match some letters:
/[a-z]/
We can match exactly 3 letters like this:
/[a-z]{3}/
...and we'll need it to be case insensitive:
/[a-z]{3}/i
...so the first part is just:
/[a-z]{3} [a-z]{3}/i
Next, we need to match either 1 or 2 numerics. A numeric can be represented with the escape sequence \d, so we'd use:
/\d{1,2}/
Next we match the time string, using the same escape sequence:
/\d{2}:\d{2}:\d{2}/
...followed by a final 4 digit year:
/\d{4}/
Put it all together and we get:
/[a-z]{3} [a-z]{3} \d{1,2} \d{2}:\d{2}:\d{2} \d{4}/i
// Fri Mar 23 15 : 21 : 08 2012
Now, we need to replace it with the current date and time. The usual place we'd go for that is the date() function, but how to we get that into the replacement dynamically? Well we could pass it as a string literal, or we could use a callback function to get it from preg_replace_callback(). But, preg_replace() gives us the e modifier which causes the replacement string to be evaluated for PHP code. We have to be careful and sparing with it's use, as with any PHP eval(), but this is a legitimate use case.
So our final PHP code looks like this:
preg_replace(
'/[a-z]{3} [a-z]{3} \d{1,2} \d{2}:\d{2}:\d{2} \d{4}/ie',
"date('D M j H:i:s Y')",
$str
);
See it working
I think listing the finite sets of options is kind of better for these task and it will also save you from false positives. These are the patterns to match each part of the date format:
Days: (?:Mon|Tue|Wed|Thu|Fri|Sat|Sun)
Months: (?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)
Day: \d{1,2}
Time: \d{1,2}:\d{2}:\d{2}
Year: \d{4}
Putting everything together:
(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun) (?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \d{1,2} \d{1,2}:\d{2}:\d{2} \d{4}
The code might look like:
$current_date = date('D M j H:i:s Y');
$text = preg_replace(
'/(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun) (?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \d{1,2} \d{1,2}:\d{2}:\d{2} \d{4}/i',
$current_date,
$text
);
See a working example.
preg_replace('/Fri Mar 23 15:21:08 2012/',date('D M d H:i:s Y'),$string);
Normally do what you want.

REGEX for date format Jun 12 2011 12:00:00:000AM

Just a quick one, what's the best REGEX to pick out dates of this format:
Jun 12 2011 12:00:00:000AM
Months are always 3 letters and days are also 2 digits.
/\w{3} \d{2} \d{4} \d{2}:\d{2}:\d{2}:\d{3}(AM|PM)/i
To capture part of it just add some parthensis whenever you need
If you need to validate a string of course don't use REGEX but http://php.net/manual/en/function.checkdate.php
try this (untested)
/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) ([0-3]\d) ([12][09]\d{2}) ([01]\d):([0-5]\d):([0-5]\d):(\d{3})(AM|PM)/

PHP: Time validation RegEx?

I am using following regex to validate date in format dd/mm/yyyy in php:
preg_match("/([0-9]{2})\/([0-9]{2})\/([0-9]{4})/", $e_startdate, $matches)
But what will be the regex to validate time in the format 10:20 PM or AM. I want a regex which will validate following format for me.
<number><number><colon><number><number><space><AM or PM>
Thanks
You can use the regex:
(0[0-9]|1[0-2]):[0-5][0-9] [AP]M
The following should validate the format you requested:
preg_match("/(\d{2}):(\d{2}) (AM|PM)/", $e_startdate, $matches);
Note that this is not necessarily a valid time. You could enter stuff like 32:62 AM. You need something like this for time validation:
preg_match("/(0?\d|1[0-2]):(0\d|[0-5]\d) (AM|PM)/i", $e_startdate, $matches);
Mind to match the whole thing case-insensitive (like i did in the second example). Otherwise lowercase am, pm, etc. are not going to work.
Regular Expression:
(([0-1][0-9])|([2][0-3])):([0-5][0-9]):([0-5][0-9])
Validates time in MySQL time format. 24 hour time colon separated hours:minutes:seconds hh:mm:ss
Matches: 09:30:00 | 17:45:20 | 23:59:59
Non-Matches: 24:00:00
/\d{2}:\d{2} (AM|PM)/
I started with elusive's answer, but needed a bit more flexibility. This one makes the minutes optional, as well as the space before AM/PM
/(0?\d|1[0-2])(:(0\d|[0-5]\d))?[ ]?(A|P)M/i

Categories