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.
Related
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 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.
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.
I know this is a common problem, but I can't seem to find the solution anywhere. In my last question [complicated date functions - comparing, subtracting I needed to compare timestamps to get an accurate date for some Cisco logs.
The best I can come up with (since the dates don't actually feature the year) is in the format
Mar 1 2013 00:03:55:
from
Mar 1 00:03:55:
But when I ran some tests, strtotime is converting this date as
Jan 1970
Using
print date("M Y", strtotime($c_log))."\n";
Am I going to have to reformat it into a date it can understand? I don't appear to have the DateTime function. What's the simplest way?
Use the date_parse_from_format() function so you can specify the format
http://php.net/manual/en/function.date-parse-from-format.php
Works for me:
echo date('M Y', strtotime('Mar 1 00:03:55')); // outputs Mar 2013
it should default to the current year when one isn't included.
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));