I'm calling a SOAP web service that expects to receive a date/time in XMLGregorianCalendar format. So, for example, I would need to convert YYYY-MM-DD HH:MM:SS to that format.
When I retrieve data from the web service, I would need to convert date/time from XMLGregorianCalendar format to YYYY-MM-DD HH:MM:SS.
I would normally post some code that I have tried but, in all honesty, I'm having trouble locating anything to even get me started with PHP. So my apologies for the lack of sample code. Would REALLY appreciate any guidance.
The date format used in XML is ISO-8601. You can use strftime to convert it to a Unix timestamp and date to convert it back to a nice readably sting.
These format string might come handy:
"Y-m-d H:i:s" sample: "2014-06-27 20:14:49"
"c" sample: "2014-06-27T20:14:49+00:00"
You can check supported time formats of strtotime here:
http://de3.php.net/manual/en/function.strtotime.php
And syntax for date here:
http://www.php.net/manual/en/function.date.php
$date = date("Y-m-d H:i:s", strtotime($date)); // Output as datetime used in SQL
$date = date("c", strtotime($date)); // Output as datetime used in XML
Related
I have a lot of variables of dates and times and I need to setTimezone in an abstract file. So I don't know anything about what is my times or dates format.
How I can keep the same format after changing the timezone? Or is there any way to find what is my date and time format?
For example, I have a timestamp like Y-m-d H:i and in my abstract class I'm changing the time zone by Carbon::parse($timestamp)->setTimezone($userTimezone) but with this line of code I'm losing my timestamp format and this code should convert multi-format of date and times. Another timestamp may have a format like Y/m/d H:I:s
I need something like this:
$format = Carbon::parse($timestamp)->getFormat(); // Returns 'Y-m-d H:i'
Or this can help; How I can setTimezone of a string timestamp without changing the format? date_default_timezone_get and date_default_timezone_set is not a good idea for this question.
You can simple change to:
Carbon::parse($timestamp)->setTimezone($userTimezone)->format('Y-m-d H:i');
I'm working with an XML document that is returning variables and for some reason in a xml return the timestamp is formatted like this... 20180606T110000 ... why anyone would format it like that makes no sense to me; however, its what I have to work with. ITs formatted YYYYMMDD , the T is the split between date and time, HHMMSS. ITs set up in a 24 Hour clock that I also need to convert to 12 hr clock with am/pm
I need that formatted like 06/06/2018 11:00:00 AM.
Is there a way to do that via a date format (I know how to use date() but I don't know how to bring in that timestamp the way its formatted) or even separating it out into
$year = xxxx
$month = xx
$day = $xx
$Hour=xx
etc. etc. etc.
if need be.
I've briefly looked at php's date create from format ( date_create_from_format('j-M-Y', '15-Feb-2009') ) but dont fully understand how that works.
I've also thought about a split. I've also looked at chunk_split and wordwrap but its not even amounts of characters so that would be complex to create.
Any ideas?
The format you're working with is "XMLRPC (Compact)" format. This is fully supported by PHP (you can see a list of supported formats here). To get what you want, just use a combination of strtotime() and date().
$timestring = "20180606T110000";
$timestamp = strtotime($timestring);
echo date("m/d/Y h:i:s A", $timestamp);
You can use PHP DateTime to parse a datetime String with any format. Please view the Parameters format in the following link to understand how the "Ymd\THis" part works: http://php.net/manual/en/datetime.createfromformat.php
<?php
$time = "20180606T110000";
$date = DateTime::createFromFormat("Ymd\THis", $time);
// 06/06/2018 11:00:00 AM.
echo $date->format("d/m/Y h:i:s A");
Paypal returns a timestamp of the following format:
yyyy-MM-ddTHH:mm:ssZ
And I don't quite know what to do with it...
How can I convert it to yyyy-MM-dd HH:mm:ss using my local timezone in php?
I'm tempted to preg_replace the mysterious letters, but something tells me there must a better way. There also appears to be 8 hours difference to my zone which I'm not sure how to substract.
Use DateTime class to do your magic.
$date = new DateTime('2012-09-09T21:24:34Z');
$date->format('Y-m-d'); # read format from date() function
You can use strtotime() to get a UNIX timestamp. From there you can do whatever you need: DateTime object, date(), etc.
Example with date():
echo date('r', strtotime('2012-09-10T10:00:00Z'));
The server's locale is set correctly, on other domains it seems to be fine... however I am currently experiencing a strange problem.
If I do a straight strtotime('now'); it returns the right timestamp (for today's date/time in my timezone) however if I do:
strtotime('07/09/2012 13:48');
It returns a timestamp that's for 9th July, like it's reading the date as a US format. I've retrieved the timezone and it is set to Europe/London (by using date_default_timezone_get).
Any ideas?
$date = DateTime::createFromFormat('d/m/Y H:i', '07/09/2012 13:48');
echo $date->format('Y-m-d H:i');
http://www.php.net/manual/en/datetime.createfromformat.php
Use the DateTime class and DateTime::createFromFormat() method.
strtotime reads it as the US format. Best to use the ISO yyyy-mm-dd.
See this for valid date formats
I want to convert date 24/09/2010 in format dd/mm/yyyy to 2010-09-24 in format yyyy-mm-dd.
This works:
date("Y-m-d",strtotime("09/24/2010"));
But this does not:
date("Y-m-d",strtotime("24/09/2010")); // it returns '1970-01-01'
Any idea why?
according to php, the valid php formats are stated here. So basically what you gave is invalid.
Alternatively, you can use mktime, date_parse_from_format or date_create_from_format
strtotime does its best to guess what you mean when given a string, but it can't handle all date formats. In you example, it is probably thinking that you are trying to refer to the 24th month, which isn't valid, and returns 0, which date then treats as the unix epoch (the date you got).
you can get around this using the mktime() and explode() functions, like so:
$date = "24/09/2010";
$dateArr = explode("/",$date);
$timeStamp = mktime(0,0,0,$dateArr[1],$dateArr[0],$dateArr[2]);
$newFormat = date("Y-m-d",$timeStamp);
As you say,
date("Y-m-d",strtotime("09/24/2010"))
will work,because the date format--"09/24/2010"is correct,
but "24/09/2010" is not the correct date format.
you can find something useful here