Re: [Regular expression] Format string to "DD/MM/YYYY hh:mm"

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Balazs Hegedus ha scritto:
<?php

    $date = '30/03/2983 12:00';
$pattern = '[0-3][0-9]/[0|1][0-9]/[1|2][0-9]{3,3}\s[0-2][0-9]:[0-5][0-9]';


I found this:

^((((([0-1]?\d)|(2[0-8]))\/((0?\d)|(1[0-2])))|(29\/((0?[1,3-9])|(1[0-2])))|(30\/((0?[1,3-9])|(1[0-2])))|(31\/((0?[13578])|(1[0-2]))))\/((19\d{2})|([2-9]\d{3}))|(29\/0?2\/(((([2468][048])|([3579][26]))00)|(((19)|([2-9]\d))(([2468]0)|([02468][48])|([13579][26]))))))\s(([01]?\d)|(2[0-3]))(:[0-5]?\d){2}$


But it accepts second too...can you help me to modify it?

bye and thanks anyway,


damn thats ugly..


Personally I love regex, as some of you on this list may have found out the hard way..


But in a situation like this where I'd also want to verify the integers passed I'd simply use the regex to help me split the given datetime string into a 5/6 element array which I can then perform tests with

Lets not forget:
 bool checkdate ( int month, int day, int year )

http://php.mirrors.ilisys.com.au/manual/en/function.checkdate.php

Helps to ensure the date at least is a valid date and day for the given month..

Therefore I would do something like:


$dtStr = preg_replace("/^(\d{2})\/(\d{2})\/(\d{2,4}) (\d{1,2}):(\d{2})$/", "$1-$2-$3-$4-$5", $date);
$dtArr = split("-",$dtStr);

if(checkdate($dtArr[1], $dtArr[0], $dtArr[2]) AND $dtArr[3]<=23 AND $dtArr[3]>=0 AND $dtArr[4]<=59 AND $dtArr[4]>=0) {
 //date and time are valid. continue processing..
}


its fairly short, has room to grow [ie changing format acceptance in regex] to allow for different seperators or even addition of seconds later down the track easily without having to dive into a untidy regex pattern.

Let me know how you go...



Best of luck!

---oOo--- Allowing users to execute CGI scripts in any directory should only be considered if: ... a.. You have no users, and nobody ever visits your server. ... Extracted Quote: Security Tips - Apache HTTP Server ---oOo--- ------oOo---------------oOo------ Julien Bonastre [The_RadiX] The-Spectrum Network CEO ABN: 64 235 749 494 julien@xxxxxxxxxxxxxxxx www.the-spectrum.org ------oOo---------------oOo------


--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.1.384 / Virus Database: 268.3.3/298 - Release Date: 30/03/2006

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux