Page 1 of 1

date pattern

Posted: Mon Apr 19, 2010 2:14 am
by m2babaey
Hi
I have read some tutorials for regular expressions but I'm still unable to detect date patterns
for example I have a string like:

Code: Select all

$string="<updated>2010-04-19T05:17:00+00:00</updated>";
I need to capture "2010-04-19T05:17:00+00:00" in $match[] using preg_match where <updated> </updated> is presented
How can I achieve that?
Thanks for your help

Re: date pattern

Posted: Mon Apr 19, 2010 5:17 am
by requinix
Are you using any kind of XML parsing library, or do you only have that string and nothing else?

Re: date pattern

Posted: Mon Apr 19, 2010 5:25 am
by m2babaey
$string is actually the result of get_file_content and is longer than above

Re: date pattern

Posted: Mon Apr 19, 2010 7:57 am
by requinix
Then without some easier method,

Code: Select all

preg_match('#<updated>([\dT:+-]+)</updated>#', $string, $matches)
$matches[1] is the date. You can use strtotime to get a timestamp - for use with date() and whatnot.