Convert datetime to include utc timezone php - php

I am trying to convert datetime to a UTC timecode using PHP, how do I convert 2022-05-14 13:30:30 so it will come out as 2022-05-14T13:30:30+01:00
Thanks

The format can be obtained with the method toAtomString(). With my timezone settings, i got the timezone 02:00, so i had to use setTimezone() and specifying the timezone on the parsing. Can be left out, if yours is correct.
Carbon::parse('2022-05-14 13:30:30', 'Europe/london')
->setTimezone('Europe/london')
->toAtomString();
This will format the date as follows 2022-05-14T13:30:30+01:00.

Simple solution with DateTime:
$date = date_create('2022-05-14 13:30:30', new DateTimeZone('Europe/London'))
->format(DateTimeInterface::ATOM);
echo $date; //2022-05-14T13:30:30+01:00
Try 3v4l.org.

Related

Carbon parse Iso 8601 string to UTC date and record it to db

I have the following Iso8601 date-time string 2018-03-12T10:34:15-0200 and after I parse it
Carbon::parse("2018-03-21T10:34:15-0200", 'UTC')
and save it to mysql db datetime column I have 2018-03-21 10:34:15 so I've lost the -0200 hours difference with UTC timezone.
Any ideas how to solve it the right way?
You don't need to pass time zone as a second parameter to parse function. Time zone is already part of date string. If you need to save date in UTC just convert it to UTC timezone after parsing like so:
Carbon::parse("2018-03-21T10:34:15-0200")->setTimezone('UTC')
Converted date will be: 2018-03-21 12:34:15.0 UTC (+00:00)
You can try something like this:
Carbon::parse("2021-12-23T07:42:42.393Z")->setTimezone('UTC')->format('Y-m-d H:i:s');
And the result will be like 2021-12-23 07:42:42.

PHP ISO 8601 format conversion issue

I want to convert this string '2016-05-30 15:35:00' into ISO 8601 format which should look like this:
Expected result : 2016-05-30T15:35:00+05:30
I tried following, but in the output it's 2 hours less,
$phpdate = strtotime('2016-05-30 15:35:00');
echo gmdate('Y-m-d\TH:i:s\+05:30', $phpdate);
Output : 2016-05-30T13:35:00+05:30
Have I made a mistake somewhere? or is there an easier way for this conversion?
You're using gmdate function of PHP which converts date into UTC format instead you need to use date function like as
date_default_timezone_set("Asia/Kolkata");//Set your timezone to Asia/Kolkata
$phpdate = strtotime('2016-05-30 15:35:00');
echo date('c', $phpdate);// 2016-05-30T13:35:00+05:30 if your timezone is of Asia/Kolkata
for an easier way to handle dates i recommend Carbon
for your problem it would be :
$date = new Carbon("2016-05-30 15:35:00");
echo $date->toIso8601String();

Error in strtotime() conversion in PHP

My variable $createdDate has a value '2014-06-23T01:44:22Z' something like this in it. It's fetched from database.
And when I convert it into standard format using strtotime(), the time returned is always 2 hours different(ahead) from the orginal variable's time.
$time1 = strtotime($createdDate);
$cretime_formated=date("Y-m-d h:i:s",$time1);
Output of $cretime_formated is 2014-06-23 03:44:22 instead of 2014-06-23 01:44:22. Why is there a difference of 2 hours in the time??? Is there anything wrong with my conversion process??
Given that the date is in Zulu timezone (and assuming all such timestamps are in that same timezone), you would need gmdate() to format it:
$cretime_formated = gmdate("Y-m-d h:i:s",$time1);
I think you have the time difference .You need to set it in UTC
date_default_timezone_set('UTC');
See Complete Refrence
List of Supported Timezones

How to convert yyyy-MM-ddTHH:mm:ssZ to yyyy-MM-dd HH:mm:ss?

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'));

Weird strtotime() issue - reading as incorrect format

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

Categories