How to make date time like this? - php

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));

Related

PHP convert string with weeks and short month name to date

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.

Convert string to dateTime for mySQL database using php

I'm a computer science student and I have been stumped on part of my assignment. We have to parse an XML file and save it in a mySQL database. The problem I have run into is that there is an element in the XML file called pubDate that has a string that is formatted like this:
Mon, 04 Nov 2013 11:08:58 -0600.
I have looked on multiple websites and have not been able to find any way to convert that string to dateTime.
Any help would really be appreciated.
Thanks
You can use like this
Example #1 DateTime::setTimeZone() example
Object oriented style
$date = new DateTime( '2008-02-07 16:45:58', new DateTimeZone('Pacific/Nauru'));
echo $date->format('D, d M Y G:i:sP') . "\n";
Output is like this
Thu, 07 Feb 2008 16:45:58+12:00
Please see this link also http://us1.php.net/datetime.settimezone.php
you can try strtotime() function to convert to unix time stamp and then strftime() to convert to mysql date format
try strtotime() function for php, if your date time is store in varchar, char or text in mysql use mysql STR_TO_DATE() function.
Take a look at the functionality provided with PHP's new Date/Time class. As long as you have the originating format of the string, you can easily convert it to an object and then back to whatever string format you need.
$d = DateTime::createFromFormat("D, d M Y h:i:s P", "Mon, 04 Nov 2013 11:08:58 -0600");
Then export it as you see fit.

PHP: Convert "13 September 2013 - 23:55" to SQL Server formatted datetime format

Trying to get my head around this, but can't seem to figure it out. I'm using PHP and attempting to convert the user-submitted date/time selection which outputs:
13 September 2013 - 23:55
I would like to convert that to the standard SQL Server format like:
2013-09-13 23:55:00.000
I've messed with the PHP strtotime() function sending it only the "13 September 2013" part, but it only outputs a long (seemingly) random number.
Is there any easier method for this?
Have a go with:
$date = DateTime::createFromFormat('d F Y - H:i','13 September 2013 - 23:55');
echo $date->format('Y-m-d H:i:s');
This lets you specify a format to read from.
strtotime returns the unix timestamp, you need to turn it to date string.
php > echo date('Y-m-d H:i:s', strtotime('13 September 2013 23:55'));
2013-09-13 23:55:00
You have to make two separate functions,
for converting month to numeric
year to two digit number
and after that you can break the user input to its desired three parts.

RFC-822 DateTime Formatting in PHP with Database [duplicate]

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

Convert time from JSON string with PHP

I have an issue with converting time string I get from JSON to another format. Somehow the date is set to minus 24 hours.
Here's object from JSON
[date] => 2011-07-02T00:00:00+02:00
I'm using strtotime() and date()
date('l, d F Y', strtotime($day->date));
But the output looks like this
FRIDAY, 01 JULY 2011
Obviously the date in JSON is Second of July. Does anyone have any idea why this happens? Am I missing something important? Will really appreciate any help!
I think you should use DateTime. It does not depend on hosts TimeZone. Beside the format is valid ISO8601. So DateTime would have not problem at all.
$dt = new DateTime("2011-07-02T00:00:00+02:00");
echo $dt->format("l, d F Y");
// Echos Saturday, 02 July 2011
http://ideone.com/yPp4d
PHP doesn't understand an infinite array of time/date strings. What is 'obvious' to a human, is not so obvious to a computer. Without a specific parser for that exact date format, how is the computer language to understand what the T in your example is for??
PHP strtotime formats will show you what formats PHP can convert a string from, to a time or date object.
Even as the date/time is parsing correctly, your tzcorrection of +0200 is telling PHP to correct for a timezone difference of GMT + 2 hours, which is likely not your correct timezone offset and thus giving you the error.

Categories