PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Thu Sep 20, 2018 10:34 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Sat Feb 26, 2011 11:53 pm 
Offline
Forum Newbie

Joined: Sat Feb 26, 2011 11:26 pm
Posts: 2
Location: New Zealand
Hi,

I've not posted on here before, but have used the forum as a resource in the past. Usually I like to figure things out for myself but this one has me confused.

I have a fair amount of experience with php but up until now have not needed to use regex (I did use it with Perl around 2000/1 but have forgotten everything).

Anyway the issue is, I have a date format '2011-12-31' which I believe should match the pattern below - problem is it doesn't!

Does anyone have any ideas?

Syntax: [ Download ] [ Hide ]
if (preg_match('((19|20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|[12][0-9]|3[01]))',$date_from)) {
            $valid_input = 1;
            echo "<p>Date From is valid</p>";
}  else {
            echo '<p class="red_text">Date From is invalid.</p>';
            $valid_input = 0;
}
 


Top
 Profile  
 
PostPosted: Sun Feb 27, 2011 12:04 am 
Offline
Moderator
User avatar

Joined: Mon Nov 03, 2003 7:13 pm
Posts: 5978
Location: Odessa, Ukraine
preg_match needs pattern separators, like this (slash being a separator): preg_match('/something/', ...)


Top
 Profile  
 
PostPosted: Sun Feb 27, 2011 2:37 am 
Offline
Forum Newbie

Joined: Sat Feb 26, 2011 11:26 pm
Posts: 2
Location: New Zealand
Brilliant! Thanks for that, this did the trick (also added the ^ and $ to prevent any text being entered before/after the date):
Syntax: [ Download ] [ Hide ]
preg_match('/^((19|20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|[12][0-9]|3[01]))$/',$date_from)
 


Top
 Profile  
 
PostPosted: Sun Feb 27, 2011 4:02 am 
Offline
Briney Mod
User avatar

Joined: Mon Jan 19, 2004 7:11 pm
Posts: 6445
Location: 53.01N x 112.48W
If you're trying to validate a date, this won't work - I could run "2011-02-31" and it would match.

Your best bet is to extract the year, month & day, and throw it at checkdate().

_________________
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.


Top
 Profile  
 
PostPosted: Mon Feb 28, 2011 12:21 pm 
Offline
Forum Contributor
User avatar

Joined: Sun Jul 05, 2009 10:39 pm
Posts: 214
Location: SLC, UT
Actually, I just ran your original code snippet and it works just fine (it is perfectly valid to use parentheses for the regex delimiter). And there is nothing wrong with the regex. The only critique I have is that you use a character class with just one character in it (i.e. '[-]'). In this case, you can simply use '-'. However, both syntaxes are equally valid with regard to matching.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: Google [Bot] and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group