This question already has answers here:
Conversion from MySQL date to RFC822 date format
(2 answers)
Closed 9 months ago.
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.
Using the following syntax, you can display current time in RFC822 Format.
$date = new DateTime('2000-01-01');
echo $date->format(DateTime::RFC822);
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.
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.
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
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!
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.)
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
You can save it as TimeStamp in database and show it RFC822 format
date(DATE_RFC822, time());
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.
don't use T at the end but an "O", it works for me
Related
I can't seem to get this date format correct.
I'm looking to display the current date like this:
Wednesday, May 16th
Any help would be appreciated.
Thanks :)
echo date('l,M jS');
php manual
http://php.net/manual/en/function.date.php
If you want leading zeroes (e.g. "Wednesday, May 02nd"):
date("l, F sS");
If you do not want leading zeroes (e.g. "Wednesday, May 2nd"):
date("l, F jS");
I am converting two strings from one date format to another but I am getting some unusual responses
The two dates i am converting are:
1: 2002-09-23
2: 2010-03-25
The PHP code is as follows for each of the dates:
1:date('d F Y', strtotime((string)$report_display->arr_output_base['Date1']['value']));
2:date("d F Y", strtotime((string)$report_display->arr_input_base['Date2']['value']));
The responses I'm getting are as follows:
1:Sometimes 31 December 1986 which is wrong but then other times I'll get 23 September 2002 which is right
2: is always 25 March 2010
When you know the format, why take risks sending it to strtotime and not use a proper method that uses the exact format?
$date = DateTime::createFromFormat('Y-m-d', '2002-09-23');
echo $date->format('d F Y');
That way there is no guesswork involved as to whether a month is a month or it is a day. That will always return the same date no matter what.
I am receiving JSON data with a date string with this format:
'Mon Jun 30, 2014'
What would be the way to convert this to a datetime? Checking the PHP functions for this I got unsecure if better to use date_parse_from_format or date_create_from_format.
What are the differences and which would suit better for this task?
DateTime::createFromFormat would work well here. It allows you to format the string easily and also takes timezones into consideration when appropriate and can be easily used in comparisons without having to convert to a timestamp first.
$date = DateTime::createFromFormat('D M d, Y', 'Mon Jun 30, 2014');
echo $date->format('Y-m-d');
You can convert to a unix timestamp using strtotime(). I don't know what you mean by a "datetime", but if you mean something like for MySQL then you format the timestamp with date() (you can include time but it isn't present in the original string):
echo date('Y-m-d', strtotime($string));
The second of the two likely fits you better ---
The first one only breaks down the date into an array, so you can work with the parts, individually.
But the second returns the DateTime object you are looking for.
I'm trying to convert a time format in the format below to a unix timestamp using PHP
j n Y H:i:s
Im trying to find a way to convert to a unix timestamp so it can be used in SQL databases. An example of the dates that I need to convert:
28 Mar 12 16:37:34
I've tried functions called "strptime" and "mktime" that I found on stackoverflow to no success - im not really sure what Im doing with them. If this is the answer here, could someone explain how to use them? Ive tried to understand the PHP documentation but Im just not getting it.
The post I was reading is here: PHP date format converting
echo strtotime('28 Mar 12 16:37:34'); //1332945454
http://php.net/manual/en/function.strtotime.php
If you need ultimate flexibility on parsing the format, use DateTime::createFromFormat()
$dt = DateTime::createFromFormat(
'j M y H:i:s', $dateString, new DateTimeZone('Your/Timezone'));
$timestamp = $dt->getTimestamp();
The php way is to use date() and strtotime()
sql uses YYYY-MM-DD HH:MM:SS
$dateTime = date('Y-m-d H:i:s', strtotime('28 Mar 12 16:37:34'));
Hi I am saving data from rss feed url. From that me got date time like this.
Sun, 2 January 2011 03:04:02 GMT+5:30
How to change this date to this format 2nd January 2011, 03:04 PM using php?
any body knows the solution please help me.
You can se the strtotime function to convert the existing string and the 'r' specifier to the date function as follows (looks like you want it in RFC 2822 format, if not tweak accordingly):
date('r', strtotime("Sun, 2 January 2011 03:04:02 GMT+5:30"));
Incidentally, make sure you're setting your local timezone correctly via date_default_timezone_set, etc.
The following functions are useful for taking a string and getting a timestamp back:
strtotime()
DateTime::createFromFormat()
After you have it as a timestamp, you can reformat it using date(). I'm not 100% sure if strtotime() would accept that format, but it should accept it because the format it isn't ambiguous.
echo date("js F Y, h A", strtotime($oldDate));