PHP converting unknown datetime string to datetime object - php

I am having trouble with the following string which is supposed to be a datetime (response from an api).
2013-03-08T19:11:11.234+0000
I need to convert the string to ISO 8601.
The exact format should look like:
2016-03-01T12:00:00+00:00
If someone could help me do this in php would be great but I would be already more than happy if someone could identify the format (like yyyy-mm-ddThh:mm:ss).

It seems the API returns the time with milliseconds.
Try this:
$date = '2013-03-08T19:11:11.234+0000';
$format = 'Y-m-d\TH:i:s.uO';
$dt = DateTime::createFromFormat($format, $date);
echo $dt->format(DateTime::ISO8601);
Result:
2013-03-08T19:11:11+0000
EDIT
Based on comments on your answer, you need to convert such a time:
2016-03-01T11:01:51.126044 00:00
Since it's not possible to parse this format with DateTime, you need to make it readable first.
There are many ways to do this. What we need is to add a + before timezone, so that my code above work.
If you are sure the date you get from the API is always this format, and that there will always be only 1 space in the string, just replace the string with a space:
$date = '2016-03-01T11:01:51.126044 00:00';
str_replace(' ', '+', $date);
$format = 'Y-m-d\TH:i:s.uP';
$dt = DateTime::createFromFormat($format, $date);
echo $dt->format(DateTime::ISO8601);
This should work (note that here the timezone mask is P since there is a colon)
You can also use preg_replace() instead of DateTime, if you don't need the date for calculations:
$date = '2016-03-01T11:01:51.126044 00:00';
$date = preg_replace('`(\.[0-9]+\s)`', '+', $date);
echo $date;
This produces:
2016-03-01T11:01:51+00:00
If you need the date for calculation, just convert it to DateTime after:
$date = '2016-03-01T11:01:51.126044 00:00';
$date = preg_replace('`(\.[0-9]+\s)`', '+', $date);
$date = DateTime::createFromFormat(DateTime::ISO8601, $date);
var_dump($date);
This produces:
object(DateTime)#1 (3) {
["date"]=>
string(26) "2016-03-01 11:01:51.000000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+00:00"
}

I suggest you to use Carbon: http://carbon.nesbot.com/docs/
echo Carbon::parse('2013-03-08T19:11:11.234+0000')->toAtomString();
Also, the format you want is ATOM, not ISO 8601. You could do #Arcesilas way, it should work too.

Related

Which date format is this?

I want to convert this date-time string 2022-09-30T21:39:25.220185674Z to yyyy-mm-dd hh:mm:ss but it returns 1970-01-01 01:00:00 everytime.
Tried with: date('Y-m-d H:i:s', strtotime('2022-09-30T21:39:25.220185674Z')); or date('Y-m-d\TH:i:s', strtotime('2022-09-30T21:39:25.220185674Z'));
Can you help find out which format this is and how i could corretly format such string in PHP?
Went through this question or this one couldn't help.
It's a ISO 8601 datetime string with microseconds, where Z is the timezone "Zulu" or UTC + 0 hours.
ISO 8601 can be parsed with DateTime() like this:
$string = '2022-09-30T21:39:25.220185Z';
//create DateTime object
$date = date_create_from_format( "Y-m-d\TH:i:s.uP" , $string);
echo $date->format( 'Y-m-d H:i:s.u' );
However This will not work with your string, as the u parameter in the format "Y-m-d\TH:i:s.uP" which represents the microseconds, in PHP takes a maximum of 6 digits, and yours has 9.
You can resolve this by removing all above 6 digits from the microseconds part of the string with a regex, like
$string = '2022-09-30T21:39:25.220185674Z';
$new_string = preg_replace( '/^.*?\.\d{0,6}\K\d*/' , '' , $string );
$date = date_create_from_format( "Y-m-d\TH:i:s.uP" , $new_string );
echo $date->format('Y-m-d H:i:s.u');
Output: 2022-09-30 21:39:25.220180
The regex explained:
1. ^.*?\.\d{0,6} // select from the begin everything including the dot
// and max 6 digits
2. \K // forget the previous match and start again from the
// point where 1. ended
3. \d* // select all digits left
4. replace the match with ""
With '?' in the format, all digits after the 6th digit can be cut off.
$string = '2022-09-30T21:39:25.220185123Z';
$date = date_create_from_format( "Y-m-d\TH:i:s.u???P" , $string);
var_dump($date);
https://3v4l.org/Upm6v
As of PHP version 8.0.10, strings like '2022-09-30T21:39:25.220185674Z' are recognized by DateTime without any problems.
$str = '2022-09-30T21:39:25.220185674Z';
$d = new DateTime($str);
var_dump($d);
/*
object(DateTime)#1 (3) {
["date"]=>
string(26) "2022-09-30 21:39:25.220185"
["timezone_type"]=>
int(2)
["timezone"]=>
string(1) "Z"
}
*/
https://3v4l.org/pI4kO

UTC date not changing in IST date format in PHP

As based on REST API page documentation the date is in UTC format as yyyy-MM-dd’T’HH:mm:ss (Eg: 2017-01-02T08:12:53)
When I hit the API, I am getting the date as 1619693307000. Converted this date using strtotime() in PHP.
What is the correct way to convert this date in d-m-Y h:i:s IST in PHP.
I used this code to do the same.
<?php echo date("Y-m-d h:i:s", '1619693307000') ?> //OUTPUT: 53296-01-14 01:00:00
The above output is absolutely wrong.
Confusion is to correctly convert UTC to IST zone and what should i do to see the correct output as the date in PHP. I read all threads on this StackOverflow and Google.
but all not helpfull.
Please help...
Your timestamp has too many digits, so it's likely in milliseconds. This seems to be a common javascript thing. So divide by 1000, and I highly recommend using the DateTime objects/interfaces rather than the old-style strtotime()/date()/etc functions.
$millis = 1619693307000;
$seconds = $millis / 1000;
$t = new DateTime('', new DateTimezone('Asia/Kolkata'));
$t->setTimestamp($seconds);
var_dump(
$t->format("Y-m-d h:i:s T")
);
Output:
string(23) "2021-04-29 04:18:27 IST"
Also "UTC" is not a format, it's a timezone. 2017-01-02T08:12:53 is an ISO8601-format. It also has a handy format shortcut:
var_dump(
$t->format("c")
);
Output:
string(25) "2021-04-29T16:18:27+05:30"
Edit: Different timezone and format:
$t = new DateTime('', new DateTimezone('UTC'));
$t->setTimestamp($seconds);
var_dump(
$t->format("Y-m-d\Th:i:s")
);
Output:
string(19) "2021-04-29T10:48:27"

How to convert a Datetime string to standard mysql

I made a function in php to convert date and time coming from a txt to the mysql standard.
But she is turning the month wrong.
I have tried all these conversions but to no avail.
I would like your help because I don't know what else to do.
function convertstringdate('05/02/202116:43:49'){
$date = new DateTime($datetime);
return date_format($date, "Y-m-d H:i:s");
}
or
$input = '05/02/202116:43:49';
$date = strtotime($input);
echo date('Y-m-d H:i:s', $date);
Since you know the format of your datetime-string I suggest you use createFromFormat() like so:
$string = '05/02/202116:43:49';
$dateTime = DateTime::createFromFormat('d/m/YG:i:s', $string);
var_dump(date_format($dateTime, "Y-m-d H:i:s"));
// output: string(19) "2021-02-05 16:43:49"
Note that I am not sure if 05 or 02 is supposed to be the month in your example, so if this seems wrong to you you might just have to switch around d/m in the format string and make it m/d.
For explanation what character means what poriton of the datetime, see the linked above documentation reference.

How can I convert a epoch time stamp to a UTC format like 2014-06-25T14:38:52.359Z in PHP

I am looking to convert an EPOCH timestamp (like 1372190184) to a format 2014-06-25T14:38:52.359Z.
I have tried the following code, but the format I get is different from what I need.
$start = new DateTime(date('r', '1372190184'));
$startDateText = $start->format('Y-m-dTH:i:sZ');
var_dump($startDateText);
exit();
But I get the output as string(30) "2013-06-25GMT+020021:56:247200" which is different from what I expect.
You forgot the backslashes in your format, and the dollar sign before startDateText in the dump:
$start = new DateTime(date('r', '1372190184'));
$startDateText = $start->format('Y-m-d\TH:i:s\Z');
var_dump($startDateText);
Also, if you're looking for microseconds, add the u format character.
You should be setting the date_default_timezone_set to UTC for your desired output. Format as you wish. And make sure to escape special characters in the format.
date_default_timezone_set('UTC');
$epoch = 1340000000;
echo gmdate('r', $epoch);
You can convert to UTC format date from a date string, for example:
$date = '2022-05-02 11:50:00';
$date = date('Y-m-d\TH:i:s\Z', strtotime($date));
echo $date;

PHP DateTime::createFromFormat doesn't parse ISO 8601 date time

Code speaks a million words:
php > echo strtotime("2010-12-07T23:00:00.000Z");
1291762800
echo date('c', 1291762800);
2010-12-08T00:00:00+01:00
php > var_dump(DateTime::createFromFormat('c', "2010-12-07T23:00:00.000Z"));
bool(false)
php > var_dump(DateTime::createFromFormat(DateTime::ISO8601, "2010-12-07T23:00:00.000Z"));
bool(false)
Any idea what's going on?
Btw, yes, new DateTime("2010-12-07T23:00:00.000Z") works fine. But I prefer to know what input I am getting.
There's a bug report that exactly describes your problem :)
https://bugs.php.net/bug.php?id=51950
Since 2016-08-07, the bug report has been marked as "not a bug". You need to use strtotime or new DateTime instead.
The constants that have been defined apply to both formatting and parsing in the same way, which forces your ways.
Parsing ISO8601 date, and also switching timezone:
// create ISO8601 dateTime
$date = DateTime::createFromFormat(DateTime::ISO8601, '2016-07-27T19:30:00Z');
// set to user's timezone
$date -> setTimeZone('Asia/Singapore');
echo $date -> format(DateTime::ISO8601);
// prints '2016-07-28T03:30:00+0800'
Nobody mentioned to use DATE_ATOM which is as far as i know phps most correct implementation of ISO 8601. It should at least work for the last 3 of these:
<?php
$dates = array(
"2010-12-07T23:00:00.000Z",
"2010-12-07T23:00:00",
"2010-12-07T23:00:00Z",
"2010-12-07T23:00:00+01:00",
(new \DateTime("now"))->format(DATE_ATOM)
);
foreach($dates as $d) {
$res = \DateTime::createFromFormat(DATE_ATOM, $d);
echo "try $d: \n";
var_dump($res);
echo "\n\n";
}
?>
To be able to parse all of them i wrote a tiny function:
<?php
function parse_iso_8601($iso_8601_string) {
$results = array();
$results[] = \DateTime::createFromFormat("Y-m-d\TH:i:s",$iso_8601_string);
$results[] = \DateTime::createFromFormat("Y-m-d\TH:i:s.u",$iso_8601_string);
$results[] = \DateTime::createFromFormat("Y-m-d\TH:i:s.uP",$iso_8601_string);
$results[] = \DateTime::createFromFormat("Y-m-d\TH:i:sP",$iso_8601_string);
$results[] = \DateTime::createFromFormat(DATE_ATOM,$iso_8601_string);
$success = array_values(array_filter($results));
if(count($success) > 0) {
return $success[0];
}
return false;
}
// Test
$dates = array(
"2010-12-07T23:00:00.000Z",
"2010-12-07T23:00:00",
"2010-12-07T23:00:00Z",
"2010-12-07T23:00:00+01:00",
(new \DateTime("now"))->format(DATE_ATOM)
);
foreach($dates as $d) {
$res = parse_iso_8601($d);
echo "try $d: \n";
var_dump($res);
echo "\n\n";
}
?>
As #Glutexo mentioned it works only if there are only 1 to 6 precision digits for the decimal part, too. Feel free to improve it.
try this:
DateTime::createFromFormat('Y-m-d\TH:i:sP', $date)
It is very strange and disappointing that this bug is still actual.
Here is a right pattern for parsing date with microseconds in decimal part of seconds:
Y-m-d\TH:i:s.uO
Usage:
$dateStr = '2015-04-29T11:42:56.000+0400'
$ISO = 'Y-m-d\TH:i:s.uO'
$date = DateTime::createFromFormat($ISO, $dateStr)
Simply :
$dt = new DateTime('2018-04-07T16:32:44Z');
$dt->format('Ymd'); // 20180407
Use DATE_ATOM rather than 'c' when formatting like #Steven said. This is how you work with ISO 8601 in PHP.
<?php
$now_date = new DateTime();
$now_iso_8601 = $now_date->format(DATE_ATOM);
echo "Now in ISO 8601 format: {$now_iso_8601}\n";
$date_from_string_and_format = date_create_from_format(DATE_ATOM, $now_iso_8601);
echo "ISO 8601 formatted string, back to DateTime object:\n";
var_dump($date_from_string_and_format);
prints
Now in ISO 8601 format: 2018-09-05T08:17:35-10:00
ISO 8601 formatted string, back to DateTime object:
object(DateTime)#2 (3) {
["date"]=>
string(26) "2018-09-05 08:17:35.000000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "-10:00"
}
For the answer listed here https://stackoverflow.com/a/14849503/2425651
we can use this format "Y-m-d\TH: i: s.u+" to keep the microseconds.
$format = 'Y-m-d\TH:i:s.u+';
$value = '2017-09-21T10:11:19.026Z'; // jsDate.toUTCString();
var_dump(\DateTime::createFromFormat($format, $value));
This one works for me:
$date = (new DateTime)->setTimestamp(strtotime('2017-12-31T23:00:00.000Z'));
I've experienced this issue with POSTGRES default Time with timezone format and this was the format that fixed it for me:
Y-m-d H:i:s.uO
This works for me:
$timeStamp = "2020-12-10T14:54:25.618Z";
var_dump(DateTime::createFromFormat('Y-m-d\TH:i:s.v\Z', $timeStamp));
object(DateTime)#1 (3) {
["date"]=>
string(26) "2020-12-10 14:54:25.618000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(3) "UTC"
}
I am using follow function that allow multiple ISO8601 formats:
function fromISO8601($time, \DateTimeZone $timezone = null) {
// valid ISO time 2019-04-01T00:00:00.000+02:00
$t = \DateTime::createFromFormat('Y-m-d\TH:i:s.uO', $time) or
// ISO time without millis 2019-04-01T00:00:00+02:00
$t = \DateTime::createFromFormat('Y-m-d\TH:i:sO', $time) or
// ISO time without timezone 2019-04-01T00:00:00.000
$t = \DateTime::createFromFormat('Y-m-d\TH:i:s.u', $time, $timezone) or
// ISO time without millis and timezone 2019-04-01T00:00:00.000+02:00
$t = \DateTime::createFromFormat('Y-m-d\TH:i:s', $time, $timezone);
return $t;
}
here are all supported dates
var_dump(
fromISO8601('2019-04-01T00:00:00.000+02:00'),
fromISO8601('2019-04-01T00:00:00+02:00'),
fromISO8601('2019-04-01T00:00:00.000'),
fromISO8601('2019-04-01T00:00:00')
);
This code is benevolent for missing timezone and milliseconds and works in older php versions.

Categories