Forum Moderators: coopster

Message Too Old, No Replies

php date RFC822 quick question

         

tec4

4:29 pm on Sep 17, 2011 (gmt 0)

10+ Year Member



Trying to format my rss with the RFC822 format using php but for some reason having a little difficulty making each "item" take on it's own time from the date listed in the database, when trying to format it using date("r").

I have a compilation of items with times like:

$item->ItemDate; //which prints "Thursday, September 15, 2011 5:00 PM" format

I try using:

date("r", $item->ItemDate)

but prints every item like: "Wednesday, December 31, 1969 4:33 PM" and gives every item the same date even when contained within a foreach statement.

The slimmed down code looks as follows for the item:

<?php
foreach($items as $item) {

?>
<item>
<pubDate><?=date("r", $item->ItemDate)?></pubDate>
</item>


<?php
}
?>

So, not really sure why each item would print out all the dates like "Wednesday, December 31, 1969 4:33 PM", any thoughts?

g1smd

4:43 pm on Sep 17, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Why the heck does RSS use RFC 822 format dates? That's a bit of a throwback to last century.

I thought that almost everything (apart from email) was supposed to be using RFC 3339 format these days.

What happens if you clear the temporary variable just before restarting the loop?

tec4

4:59 pm on Sep 17, 2011 (gmt 0)

10+ Year Member



Haven't the slightest.

Well the issue is that the date is spits out isn't even correct for the first item. The first item (as it is in the DB is Thursday, September 15, 2011 5:00 PM but when i put the date("r") in there is shows that item and all others as Wednesday, December 31, 1969 4:33 PM) which i believe is when the date() starts counting?

I tried clearing the temporary variable and even defining as something else before the loops ends and didn't seem to do anything noticeable.

g1smd

5:33 pm on Sep 17, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I didn't notice the 1969 bit. That's "zero".

It means the variable you're reading is blank. Presumably you're using the wrong variable, or it hasn't got the right scope.

astupidname

2:56 pm on Sep 18, 2011 (gmt 0)

10+ Year Member



have you tried:
date("r", strtotime($item->ItemDate))

tec4

1:30 am on Sep 19, 2011 (gmt 0)

10+ Year Member



Thanks for both of your responses! Ya, guess it wasn't reading the current format, as it is in the DB.

But using the strtotime around it seemed to work like a charm :)

rocknbil

4:16 pm on Sep 19, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Trying to format my rss with the RFC822 format using php but for some reason having a little difficulty making each "item" take on it's own time from the date listed in the database, when trying to format it using date("r").


If this is an exercise in PHP dates, fair enough - but if it's coming from a database, just select it in the format you want. No need to jump through hoops in programming . . .

select date_format(now(), "%W, %M %d %Y %I:%i %p");

--> Monday, September 19 2011 09:14 AM

Substitute your field name for now().

select date_format(your_field_name, "%W, %M %d %Y %I:%i %p") as formatted_date;

Date and time functions [dev.mysql.com]