I have a date that I receive in MS format for JSON dates. It looks like this:
/Date(1365004652303)/
I can convert it to a PHP DateTime object by doing this:
$timestamp = round(((int) $originalMSdate) / 1000);
$convertedDate = new DateTime();
$convertedDate->setTimestamp($timestamp);
Ultimately, though, I need it to be a string in ISO 8601 format. I tried then converting it to an ISO date object & then converting that to a string with strval() but strval() doesn't work on date objects.
I've also tried
$dateString = date_format($convertedDate, 'YY-MM-DD H:i:s');
but I need it to also include timezone info, like this: 2015-10-01T21:22:57.057Z
I don't see characters for that in date_format.
How can I achieve this?
EDIT: I should clarify that I'm not printing the resulting string. I need to pass it to a field in a database that accepts a string datatype.
Please try the below code
<?php
// input
$time = microtime(true);
// Determining the microsecond fraction
$microSeconds = sprintf("%06d", ($time - floor($time)) * 1000000);
// Creating DT object
$tz = new DateTimeZone("Etc/UTC");
$dt = new DateTime(date('Y-m-d H:i:s.'. $microSeconds, $time), $tz);
$iso8601Date = sprintf(
"%s%03d%s",
$dt->format("Y-m-d\TH:i:s."),
floor($dt->format("u")/1000),
$dt->format("O")
);
// Formatting according to ISO 8601-extended
var_dump(
$iso8601Date
);
This worked:
$timestamp = round(((int) $originalMSdate) / 1000);
$dateString = date('c', $timestamp);
The format isn't EXACTLY the same. It's in this format:
2016-04-25T14:27:00-05:00 rather than
2016-04-25T14:27:00.057Z
but it's close enough that I can do some manipulation to get what I need.
this one is worked for me. For more please refer this article.
$date = date('Y-m-d H:m:s');
echo date('c', strtotime($date)); // 2020-04-08T16:04:56+05:30
echo date(DateTime::ISO8601, strtotime($date)); // 2020-04-08T16:04:56+0530
echo date(DateTime::ATOM, strtotime($date)); // 2020-04-08T16:04:56+05:30
Related
I'd like to convert value/date/time that I got from callback
raw value that I got is like this
$value='2021-01-20T19:03:52.355+0300';
I need to convert it into like this
$value='20-01-2021 23.03.52,355000 +07:00';
what I've done some substr and concat
but unfortunately it ends up with string and my db datatype format is timestamp and i can't insert the value to db
read some about DateTime::createFromFormat
and I can convert the time format but still no clue for converting to another timezone
You could change the timezone using setTimezone() :
$value = '2021-01-20T19:03:52.355+0300';
$expected = '20-01-2021 23.03.52,355000 +07:00';
$datetime = new \DateTime($value);
$datetime->setTimezone(new \DateTimeZone('+0700'));
var_dump($datetime->format('d-m-Y H.i.s,u P') == $expected); // bool(true)
You can try this code, this will work for you and you can set the timezone as per your need.
// Input : '2021-01-20T19:03:52.355+0300';
// Output : '20-01-2021 23.03.52,355000 +07:00';
date_default_timezone_set('Europe/London');
$datetime = new DateTime('2021-01-20T19:03:52.355+0300');
// timezone to convert.
$la_time = new DateTimeZone('Asia/Krasnoyarsk');
$datetime->setTimezone($la_time);
echo $datetime->format('d-m-Y H.i.s,u P');
Output:
20-01-2021 23.03.52,355000 +07:00
you can do like this
$datetime = new \DateTime('2021-01-20T19:03:52.355+0300');
$datetime->setTimezone(new \DateTimeZone('+0700'));
date_format($datetime, 'd-m-Y H.i.s,u P');
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.
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;
I have a problem by converting a date in proper format.
I get the time the from Facebook API in this format: 2013-08-23T09:00:00
I then $fbdate = date('2013-08-23T09:00:00');
When I echo $fbdate, it retuns 2013-08-25UTC05:00:00.
Then I tried:
$datum = date("d.m.Y",$fbdate);
$uhrzeit = date("H:i",$fbdate);
To extract the date and the time but it always returns:
01.01.1970 for $datum and 00:33 for $uhrzeit.
You should use strtotime() to parse a date string into a UNIX timestamp:
$fbdate = strtotime('2013-08-23T09:00:00');
$datum = date('d.m.Y', $fbdate);
$uhrzeit = date('H:i', $fbdate);
Try using the DateTime class:
$fbdate = '2013-08-23T09:00:00';
$date = DateTime::createFromFormat('Y-m-d\TH:i:s', $fbdate);
$datum = $date->format('d.m.Y');
$uhrzeit = $date->format('H:i');
echo $datum;
echo $uhrzeit;
$datum = date("d.m.Y",strtotime($fbdate));
$uhrzeit = date("H:i",strtotime($fbdate));
The date function in PHP is meant to convert a microtime into a date, so you need to convert your string dates to microtimes first.
Have you tried this? You should use strtotime() when using the date function.
$datum = date("d.m.Y", strtotime($fbdate));
$uhrzeit = date("H:i", strtotime($fbdate));
Use strtotime()
$a = date("Y-M-d", strtotime($datum));
echo $a.$uhrzeit;
More info http://php.net/manual/en/function.strtotime.php
I'm not a huge fan of using timestamp.
What I did to solve this issue was:
$updatedDate = new DateTime(
preg_replace('/^(.*)\+0000$/', '$1', $fbUser->getProperty("updated_time")),
new DateTimeZone("UTC")
);
It chops the +0000 part and creates the DateTime object with an explicit UTC timezone.
I am using Datetime class on PHP.
you can change datetimeclass to string like this.
$date->format('Y-m-d H:i:s')
// it shows 2013-08-05 10:00:00
but somehow ,Google API requires format like this .
2013-08-05T10:00:00
What this T means ?
and How can I make this style string from DateTime class neatly?
The time is in ISO 8601 format. To print it out, you can use 'c' format character:
$date->format('c')
You could use jh314's solution above, and it will give you the time in following format:
2013-08-08T10:18:15+05:30
However, to format it exactly like you want, you could use the following:
$part1 = $date->format('Y-m-d'); // 2013-08-08
$part2 = $date->format('H:i:s'); // 10:19:37
$newdate = "{$part1}T{$part2}"; // 2013-08-08T10:19:37
Or better yet:
$date = $date->format('Y-m-d\TH:i:s'); // 2013-08-08T10:19:37
Ta-dah!
This is ISO 8601 datetime format check this
$date->format('c') //Output 2004-02-12T15:19:21+00:00
This is almost, but not quite ISO8601 format, so you need to format the output like this:-
$date = new \DateTime();
echo $date->format('Y-m-d\TH:i:s');
The \ escapes the 'T'. See the manual about formatting dates.
See it working