Extract time from XML PUBDATE

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Mediadot
Forum Newbie
Posts: 1
Joined: Mon Jun 29, 2009 5:26 pm

Extract time from XML PUBDATE

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Extract time from XML PUBDATE

Post 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...
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Extract time from XML PUBDATE

Post 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;
Post Reply