1

I have a date field on my database. the date format as following.

June 17, 2013

Im using the format as

date("F j, Y");

So my question is there a way that i can display this date in RFC-822 format using php? or do i need to start saving the date in RFC-822 format from now on? Thanks in advance.

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
maxlk
  • 1,047
  • 6
  • 18
  • 34
  • 5
    Why don't you save the dates as ... dates? I.e., change the DB field to a date type and save yourself from conversions. Existing values can be easily parsed into "real" dates. – jensgram Jun 17 '13 at 06:56
  • What is your field type? – sectus Jun 17 '13 at 06:58

10 Answers10

6

Using the following syntax, you can display current time in RFC822 Format.

$date = new DateTime('2000-01-01');
echo $date->format(DateTime::RFC822);
DevZer0
  • 13,433
  • 7
  • 27
  • 51
5

Neither.

From now on you have to start using format supplied by database.
You have to understand the difference between storage format and display formatting. It's different matters. When storing data in mysql, you have to follow mysql rules. So, instead of June 17, 2013 you have to store 2013-06-17.

And then convert at output to whatever format required - not limited to a single one but whatever format is demanded by destination.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
3

None of the other answers worked for me, so this is what worked... to take a date in PHP and output it in RFC822:

date("D, d M Y G:i:s T", strtotime($date));

Hope that helps others.

Dan Goodspeed
  • 3,484
  • 4
  • 26
  • 35
  • 4
    Feed wouldn't [validate](https://www.feedvalidator.org/) until I adjusted you example as per [php.net](http://php.net/manual/en/class.datetime.php): 'date('D, d M Y H:i:s O',strtotime($date))' – ashleedawg Oct 14 '17 at 08:55
  • 1
    Almost perfect, just change the G: to H: to get a leading zero on the hour. – Brian Tristam Williams Jul 06 '20 at 01:58
2

As was pointed out your best bet is to change the way you are storing your dates to something other then a string. date("Y-m-d", strtotime($date)) can assist you in this endeavor.

But to solve the immediate need you can utilize use strtotime, date and the DATE_RFC822 constant to get you what you are looking for.

echo date(DATE_RFC822, strtotime($value));

See First example on php date documentation

Orangepill
  • 24,500
  • 3
  • 42
  • 63
2

As @ashleedawg and others mentioned in some comments the simplest solution that works:

date("D, d M Y H:i:s O", strtotime($date));

Mind the "H" and the "O" ;)
Thanks!

romek
  • 615
  • 1
  • 6
  • 13
2

If you want to date format something in PHP for RFC-822 , then just do this...

date('r', strtotime($date))

'r' » RFC 2822 formatted date Example: Thu, 21 Dec 2000 16:01:07 +0200

Source: PHP.net: DateFormat

But, as other stated, you don't want to store this in your database! However, you'll need to use r for other things, like XML-RSS date time formats...

All date-times in RSS conform to the Date and Time Specification of RFC 822... (Source: RSS 2.0 Specification.)

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
  • `$this->_add_hdr('Date', sprintf(date('r', strtotime($date))));` produces the: `Date: Thu, 01 Jan 1970 01:00:00 0100` idea please? – 16851556 Apr 28 '22 at 14:07
0

date_format(date(your database field), '%D, %j %M %t')

and what type of format you want just see the link date and time format for Mysql

0

You can save it as TimeStamp in database and show it RFC822 format

date(DATE_RFC822, time());
Yuseferi
  • 7,931
  • 11
  • 67
  • 103
0

This is the only solution that worked for me:

date("D, d M Y H:i:s T", strtotime($date));

Other examples above that didn't work include using the DATE_RFC822 format specifier, which puts out a 2-digit year, instead of 4 digits. Then the other suggestion to use G:i:s for time doesn't work because G specifies no leading zeroes, so you'll get 2:00:00 instead of 02:00:00.

0

don't use T at the end but an "O", it works for me