tags:

views:

349

answers:

3

I have a date variable that contains data in the forma: DD-MM-YY. -> $date

I also have another variable that contains the time in HH:MM. -> $time

I'd like to convert it to RFC-822 for to be used in a RSS feed.

How can I achieve this with PHP?

A: 
$date = "DD-MM-YY HH:MM"; // your date with or without hour and minutes

$time = strtotime($date);

$rfc_time = date("r", $time);
marvin
This doesn't work for me. With `$date = "11-12-09 12:22"`, I get the output `Fri, 09 Dec 2011 12:22:00 +0000`. `strtotime` is reading the input as YY-MM-DD.
Phil Ross
Thank you but this solution generates the following. ie:$date = "30-12-09 07:15";$rfc_time --> Mon, 09 Dec 2030 07:15:00 -0800So it doesn't work for DD-MM-YY
Cy.
then you should parse the date for yourself
marvin
+3  A: 

Try this:

function RFC2822($date, $time = '00:00') {
    list($d, $m, $y) = explode('-', $date);
    list($h, $i) = explode(':', $time);

    return date('r', mktime($h,$i,0,$m,$d,$y));
}

$date = '30-12-2009';
$time = '11:30';

echo RFC2822($date, $time);

Will output something like this:

Wed, 30 Dec 2009 11:30:00 +0200

The second parameter of the function is optional, you can supply only the date and it will still work.

Tatu Ulmanen
A: 

Try something like:

<?php
$date = '11-01-09'; // Jan 11th, 2009
$time = '21:30';

// Correct the invalid order of the date
$date_parts = array_reverse(explode("-", $date));
$date = implode('-', $date_parts);

// Set up the format
$timestamp = strtotime($date . " " . $time);
$rss_datetime = date(DATE_RFC2822, $timestamp);

echo $rss_datetime;
?>
Atli