monsterboy
monsterboy

Reputation: 153

Remove time from <pubDate> PHP RSS feed

I have records with no time attached so, with the rss feed I'm just getting 00:00.

I need help customizing this code to remove the time but, keep the date.

Any help would be great as I have been playing with this for fat too long :)

 $get_courses = "SELECT 
     courses.course_id, 
     DATE_FORMAT(courses.date_added,'%a %b %e %Y') as formatted_date_added, 
     courses.course_type, 
     courses.start_date, 
     location_name, 
     price, 
     duration 
 FROM courses 
 JOIN addresses ON courses.course_get_address_id = addresses.address_id 
 WHERE courses.active = 'Active' 
 ORDER BY courses.date_added DESC 
 LIMIT 30";

XML

 '<pubDate>'.$course['formatted_date_added'].' GMT</pubDate>'

Upvotes: 0

Views: 1663

Answers (3)

piyush
piyush

Reputation: 976

There are PHP functions to manipulate date format. I like using following code to play around with different date formats.

<?php
$date_str= "Mon Apr 23 2012";
$desired_date_format= "Y-m-d";
echo $new_date_str = change_date_format($desired_date_format,$date_str);

function change_date_format($date_format, $date_str)
{
    return date($date_format,strtotime($date_str));
}

?>

Upvotes: 0

Basti
Basti

Reputation: 4042

Change

DATE_FORMAT(courses.date_added,'%a %b %e %Y') as formatted_date_added

to

UNIX_TIMESTAMP(courses.date_added) as formatted_date_added

and in PHP:

if (date("H:i", $tuple["when"]) == "00:00")
    // without time
    echo '<pubDate>'.strftime('%a %b %e %Y', 
        $course['formatted_date_added']).' GMT</pubDate>';
else
    // with time
    echo '<pubDate>'.strftime('%a %b %e %Y %r', 
        $course['formatted_date_added']).' GMT</pubDate>';

Or just output

'<pubDate>'.strftime('%a %b %e %Y', $course['formatted_date_added']).' GMT</pubDate>'

if you won't ever have time information.

Upvotes: 1

hjpotter92
hjpotter92

Reputation: 80629

Instead of extracting date like that, better let PHP do it.

'<pubDate>'. date( "d-m-Y", strtotime( $course['formatted_date_added'] ) ) . ' GMT</pubDate>';

EDIT

So, change your SELECT as shown below:

SELECT courses.course_id, 
 courses.date_added AS formatted_date_added, courses.course_type, courses.start_date, 
 location_name, price, duration 
FROM courses <rest is fine, keep it intact>

Upvotes: 1

Related Questions