Converting UTC to CST in php - php

I want to convert a UTC time to CST using PHP. After googling I got a function
function date_convert($dt, $tz1, $df1, $tz2, $df2) {
// create DateTime object
$d = DateTime::createFromFormat($df1, $dt, new DateTimeZone($tz1));
// convert timezone
$d->setTimeZone(new DateTimeZone($tz2));
// convert dateformat
return $d->format($df2);
}
echo date_convert('2018-05-29 11:44:00', 'UTC', 'Y-m-d H:i:s', 'CST', 'Y-m-d H:i:s');
The output is
2018-05-29 05:44:00
But when I tried converting 2018-05-29 11:44:00 to UTC using online converter, I got the result as 05/29/2018 6:44 AM, which is 1 hour more than what the function returns.
Can anyone help me to find the correct output? Thanks in advance.

I can't explain it, but it only seems to happen with EST/CST, even with changing it to a D.
$n = new DateTime(); // 'date' => '2018-05-29 09:45:01.000000' America/New_York
$n->setTimeZone(new DateTimeZone('UTC')); // 'date' => '2018-05-29 13:45:01.000000'
$n->setTimeZone(new DateTimeZone('EDT')); // 'date' => '2018-05-29 08:45:01.000000'
But if you use a place instead of a timezone, it works well:
$n->setTimeZone(new DateTimeZone('America/New_York')); // 'date' => '2018-05-29 09:45:37.000000'
$n->setTimeZone(new DateTimeZone('America/Chicago')); // 'date' => '2018-05-29 08:45:37.000000'
I'd suggest using a place such as America/Chicago instead.

Try this;
function date_convert($time, $oldTZ, $newTZ, $format) {
// create old time
$d = new \DateTime($time, new \DateTimeZone($oldTZ));
// convert to new tz
$d->setTimezone(new \DateTimeZone($newTZ));
// output with new format
return $d->format($format);
}
echo date_convert('2018-05-29 11:44:00', 'UTC', 'CST', 'Y-m-d H:i:s');
Output:
2018-05-29 05:44:00
CST is UTC-06, php works well

Related

How to convert dates to specific formats in php?

I'm having trouble converting dates to php, what conversion would this date be for first? 2021-10-04T09:49:50.636-04:00.
In the documentation this format is ISO 8601, but it doesn't look like ISO 8601.
Ex:
$today = date('Y-m-d h:i:s');
$format = new DateTime($today);
$expires = $format->format(DateTime::ISO8601);
echo $expires; //2021-10-04T09:49:50-0300
Are extremely different:
2021-10-04T09:49:50-0300 ISO 8601
2021-10-04T09:49:50.636-04:00 The format I don't know
How can I convert my current date date() to this format?:
2021-10-04T09:49:50.636-04:00
DateTime automatically recognizes many date formats. So also "2021-10-04T09: 49: 50.636-04: 00" (DateTime :: RFC3339_EXTENDED).
To create a DateTime object, you don't have to know the exact name.
$strDate = "2021-10-04T09:49:50.636-04:00";
$dt = new DateTime($strDate);
/*
DateTime::__set_state(array(
'date' => "2021-10-04 09:49:50.636000",
'timezone_type' => 1,
'timezone' => "-04:00",
))
*/
Creating a DateTime object for today 00:00 is also easy:
$dt = new DateTime('today');
Examples of special output formats:
echo $dt->format(DateTime::ISO8601)."<br>";
echo $dt->format(DateTime::RFC3339_EXTENDED)."<br>";
/*
2021-10-04T09:49:50-0400
2021-10-04T09:49:50.636-04:00
*/
If you want to know what is behind a class constant like DateTime::RFC3339_EXTENDED then let it show you with PHP:
echo DateTime::RFC3339_EXTENDED; //Y-m-d\TH:i:s.vP

php , convert to date from excel

I am importing a excel , it seems from source the date is coming like this :
"2021-04-08T12:36:12+03:00"
i used the function
public function transformDate($value)
try {
return \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($value);
} catch (\ErrorException $e) {
return '-1';
}
this function did a exception ,
if i used strtotime(2021-04-08T12:36:12+03:00) , it return bad date like : 0616-05-07 00:05:00
i can't find the correct function
The date string "2021-04-08T12:36:12+03:00" contains a time zone +03:00.
The best way is to use DateTime to keep the correct time zone.
$dateTime = new DateTime("2021-04-08T12:36:12+03:00");
echo $dateTime->format('Y-m-d H:i:s O');
//2021-04-08 12:36:12 +0300
If the date is required for a different time zone, this can be done with setTimeZone().
$dateTime->setTimeZone(new DateTimeZone("Europe/Berlin"));
echo $dateTime->format('Y-m-d H:i:s e');
//2021-04-08 11:36:12 Europe/Berlin
If we use strtotime and Date then we get a date and a time according to the setting of our server (My time zone is "Europe/Berlin").
echo date('Y-m-d H:i:s e',strtotime("2021-04-08T12:36:12+03:00"));
//2021-04-08 11:36:12 Europe/Berlin

Yii2 get time as UTC

Is there an easy way to get timestamp in UTC without updating the config file?
I am able to do so by using php date_default_timezone_set.
I need this in Yii2 and new \DateTimeZone('UTC') does not seem to work.
$date = new \DateTimeZone(\Yii::$app->timeZone);
echo $date->getName().'<br>';
$date = new DateTimeZone('UTC');
echo $date->getName().'<br>';
\Yii::$app->timeZone = 'EST';
$date = new DateTimeZone(\Yii::$app->timeZone);
echo $date->getName();
Using the Yii functions
// #var string the time zone to use for formatting time and date values.
// This can be any value that may be passed to [date_default_timezone_set()](http://www.php.net/manual/en/function.date-default-timezone-set.php)
Yii::$app->formatter->timeZone = 'UTC';
// #var string the default format string to be used to format a [[asDate()|date]]. `UTC`, `Europe/Berlin` or `America/Chicago`.
Yii::$app->formatter->defaultTimeZone = 'UTC';
// or
$formatter= new \yii\i18n\Formatter;
$formatter->defaultTimeZone = 'UTC'; // ..
Usually the most convenient way to handle this is to use Formatter component. If all dates are in the same timezone, you're configuring it in only one place using Formatter::$defaultTimezone setting in your config (you may skip this in your case, since UTC is default value):
'formatter' => [
'defaultTimeZone' => 'UTC',
],
Then you can use asTimestamp():
echo Yii::$app->getFormatter()->asTimestamp($date);
You can set the default time zone in the controller action using the following code
$UTC = new DateTimeZone("UTC");
$newTZ = new DateTimeZone("America/New_York");
$date = new DateTime( "2011-01-01 15:00:00", $UTC );
$date->setTimezone( $newTZ );
echo $date->format('Y-m-d H:i:s');
OR use in any controller action
date_default_timezone_set("Asia/Bangkok");
echo date_default_timezone_get();

PHP: convert time to device timezone

I have a database table where I store a date NOW(), and the time is set two hours behind me. My timezone is UTC+2, and my phpMyAdmin database is using 'Africa/Accra' or 'Artic/Reykjavik' - GMT.
It’s fine if my database is using this as default, but when I try to convert the time to the users timezone, nothing happens.
This is what I have tried:
date_default_timezone_set("Africa/Accra"); // Also tried with GMT
$timezone = 'Europe/Berlin';
$feed_time = '2014-05-20 19:27:52';
$schedule_date = new DateTime($feed_time, new DateTimeZone($timezone) );
$newdate = date('Y-m-d H:i:s',strtotime("$feed_time UTC"));
echo $newdate;
This echo out: 2014-05-20 19:27:52, so no difference.
How can I convert the time so it would be the same as the device default timezone?
You're not using $schedule_date. You create it with your new values but never actually use it. You still use the old data.
$schedule_date = new DateTime($feed_time, new DateTimeZone($timezone) );
echo $schedule_date->format('Y-m-d H:i:s');
Also, you need to set your date/time first and then change the timezone. This is because your date/time is in UTC so you need to start there. Then you can change the timezone accordingly.
$original_date = new DateTime('2014-05-20 19:27:52', new DateTimeZone('UTC') );
$original_date->setTimeZone(new DateTimeZone('Africa/Accra'));
echo $original_date->format('Y-m-d H:i:s');
$schedule_date = new DateTime('2014-05-20 19:27:52', new DateTimeZone('UTC') );
$schedule_date->setTimeZone(new DateTimeZone('Europe/Berlin'));
echo $schedule_date->format('Y-m-d H:i:s');
Output:
2014-05-20 19:27:52
2014-05-20 21:27:52
Demo

DateTime class if not displaying the correct data

I am using DateTime class for the first time to convert between different time zones.
Here is what I have
$USER_TIME_ZONE = new DateTimeZone('America/Los_Angeles');
$UTC = new DateTimeZone('UTC');
$schedule_date = new DateTime($call['triggerOn'], $USER_TIME_ZONE);
echo $schedule_date->format('m/d/Y h:i A');
$schedule_date = new DateTime($call['triggerOn'], $UTC);
echo $schedule_date->format('m/d/Y h:i A');
Here is how I am going through my result and trying to convert them
foreach ( $activities AS $call){
$USER_TIME_ZONE = new DateTimeZone('America/Los_Angeles');
$UTC = new DateTimeZone('UTC');
$schedule_date = new DateTime($call['triggerOn'], $UTC);
echo $schedule_date->format('m/d/Y h:i A');
}
The following are value for $call['triggerOn']
2013-02-27 18:00:37
2013-02-02 01:11:07
2013-01-10 17:12:14
2013-02-27 22:29:42
2013-02-27 22:28:38
2013-02-25 21:53:12
2013-02-14 14:35:48
2012-12-13 14:03:16
2013-03-04 19:04:20
2013-03-01 18:52:48
2013-03-05 15:46:56
2013-03-11 15:26:17
2013-02-07 18:17:30
2013-03-05 18:04:25
Both of my outputs are the same! I don't understand why. Is there a configuration that I need to do on the server side as I have PHP running on Windows Server 2008 R2.
Thank you for your help and time.
The DateTime::format() method is returning the time in the timezone the data was created in. There's no conversion going on. Thus your output times are going to be the same as your input times regardless of the timezone you pass in. You can verify this by adding an 'e' to the format parameter. You will see that in the first case the timezone is America/Los_Angeles and in the second it is UTC.
You're probably trying to convert the time between timezones, right? In order to do that just create a single new DateTime object in one timezone, call the setTimezone method with the second timezone, and then format the result.
All of this assumes that the $call['triggerOn'] value is neither a timestamp nor a value with the timezone identified. In that case the second parameter of the DateTime constructor is ignored.
Knowing the value of $call['triggerOn'] would help, but would this work:
$USER_TIME_ZONE = 'America/Los_Angeles';
$UTC = 'UTC';
$schedule_date = new DateTime( $call['triggerOn'], $USER_TIME_ZONE );
echo $schedule_date->format( 'm/d/Y h:i A' );
$schedule_date = new DateTime( $call['triggerOn'], $UTC );
echo $schedule_date->format( 'm/d/Y h:i A' );
Basically, instead of creating new DateTime objects to use as parameters for other new DateTime objects, what if you just use the timezone string instead? Does that work?

Categories