Convert a date DD-MM-YY and HH:MM vars to RFC-822 using PHP
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?
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.
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;
?>
$date = "DD-MM-YY HH:MM"; // your date with or without hour and minutes
$time = strtotime($date);
$rfc_time = date("r", $time);
精彩评论