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.
Related
I have a date in this format "Wed, 26 Oct 2022 16:11:30 -1100", need to convert it to UTC time and in a format so it can be inserted into a MySQL database.
The date was pulled from an email using "$headerInfo->date;". I don't see any way to receive the date any different.
Every way I try to do the conversion is painful and brute force.
Is there an elegant, or not painful way to do this?
TIA.
Been trying regex but it doesn't handle converting the month into digits, then you have the UTC offset (time zone) to work with.
Parse the date/time string using DateTimeImmutable::createFromFormat, set the timezone to UTC then format it to MySQL's datetime literal syntax
$dt = DateTimeImmutable::createFromFormat('D, j M Y H:i:s O', $dateString);
$mysqlFormat = $dt->setTimezone(new DateTimeZone('UTC'))->format('Y-m-d H:i:sP');
Demo ~ https://3v4l.org/G7RBG
<?php
//just use strtotime
echo date('Y-m-d H:i:s e',strtotime('Wed, 26 Oct 2022 16:11:30 -1100'));
?>
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 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.
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));