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');
Related
I have this format:
How do I use PHP DateTime to format like 2023-01-27T17:37:00.000Z
So far I have
$date = new DateTime();
$string = $date->format('Y-m-dTHH:H:i:s');
but it outputs 2022-11-25UTC0000:00:00:00
What is the correct format
Is there a ressource on the web that would find it for me ? like a helper website.
You can do as follows:
$date = new DateTime();
$string = $date->format('Y-m-d\TH:i:s.000\Z');
echo $string;
You can use the DateTime class to format the date like this:
$date = new DateTime('2023-01-27T17:37:00.000Z');
echo $date->format('Y-m-d\TH:i:s.u\Z'); // Outputs 2023-01-27T17:37:00.000Z
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
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 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
I am creating a script that allows the user to choose their own timezone...
and enter the date $ time..So the user entered date/time must be converted to GMT format while storing into the database.
While retrieving from the database it should be again converted into original format.
Here DST concept must also be included.
So here date can be in a variable which can be a string or array(multidimensional array also)
So i tried like this.....
function ConvertOneTimezoneToAnotherTimezone($time,$currentTimezone,$timezoneRequired)
{
$current_zone = new DateTimeZone($currentTimezone);
//$gmt = new DateTimeZone('GMT');
$date = new DateTime($time, $current_zone);
//$date->setTimezone($gmt);
$date->setTimezone(new DateTimeZone($timezoneRequired));
return $date->format('Y-m-d H:i:s');
// Convert it back to Original timezone
$date->setTimezone($current_zone);
return $date->format('Y-m-d H:i:s');
}
$time='2011-03-29 11:15:00.000';
echo "Current Date/Time is=".ConvertOneTimezoneToAnotherTimezone($time,'Asia/Kolkata','America/New_York');
but here i am only able to convert into different timezones,but i want a single function which converts date/time and also while retrieving gives original format......
please anybody help me......
<?php
function ConvertOneTimezoneToAnotherTimezone($originalDateTime, $originalTimeZone, $targetTimeZone) {
$format = 'Y-m-d H:i:s';
$dateTime = new DateTime($originalDateTime, new DateTimeZone($originalTimeZone));
$original = $dateTime->format($format);
$dateTime->setTimezone(new DateTimeZone($targetTimeZone));
$target = $dateTime->format($format);
return compact('original', 'target');
}
$dateTime = '2011-03-29 11:15:00.000';
$converted = ConvertOneTimezoneToAnotherTimezone($dateTime,'Asia/Kolkata','America/New_York');
echo sprintf('Original Date/Time is=%s', $converted['original']), PHP_EOL;
echo sprintf('Converted Date/Time is=%s', $converted['target']), PHP_EOL;
something like this
http://service-kl.com/code/tz_demo2/?cou=USA