Page 1 of 1

Extract time from XML PUBDATE

Posted: Mon Jun 29, 2009 5:32 pm
by Mediadot
I am using a XML feed that has a tag [PUBDATE] with the follwing format ex: 29 Jun 09 : 10:12:05
I would like to extract (echo) just the time in this format 10:12.
Can anyone help me with the PHP (5) code?

I have used <?= gmdate("H:i", strtotime($item["PUBDATE"]));?> ... but it prints time 00:00
Thanks.
Joao

Re: Extract time from XML PUBDATE

Posted: Mon Jun 29, 2009 5:49 pm
by requinix
The date is literally that?

Code: Select all

29 Jun 09 : 10:12:05
And this is from an RSS feed? That's stupid. Specifications are meant to be followed, not blindly ignored.

If it always has a space+colon+space and the time is on the far side of it, use explode to split the string into date/time chunks...

Re: Extract time from XML PUBDATE

Posted: Mon Jun 29, 2009 11:10 pm
by Benjamin
If you have to, you can parse it out with regex.

Code: Select all

<?php
if (preg_match('#^\d{1,2}\s[a-z]{3,4}\s\d{1,4}\s:\s(\d{1,2}):(\d{1,2}):\d{1,2}$#i', "29 Jun 09 : 10:12:05", $matches)) {
    $time = "{$matches[1]}:{$matches[2]}";
} else {
    $time = "unknown";
}
 
echo $time;