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();
Related
I am currently working on displaying a moment. I want the view to use DateTime->format(..). The value I am getting from the API is 1502462223168. However, this is displayed as 1945-5-26 16:36 instead of 2017-8-11 16:37, since the original value exceeds the PHP_MAX_INT value on the system.
Is there a way I can use the original value, maybe as a String, to create the DateTime object?
since your timestamp value is in milliseconds, divide it by 1000 and then use DateTime, like:
$date = new DateTime();
$stamp = intval(1502462223168/1000);
$date = DateTime::createFromFormat("U", $stamp)->format("Y-m-d H:i:s");
echo $date;
Use this it will be working fine for your question
$timestamp = 1465298940;
$datetimeFormat = 'Y-m-d H:i:s';
$date = new DateTime();
// If you must have use time zones
// $date = new \DateTime('now', new \DateTimeZone('Europe/Helsinki'));
$date->setTimestamp($timestamp);
echo $date->format($datetimeFormat);
My input is a timezone value (say "america/los_angeles"), and its output should be a timezone abbreviation(say "PST").
Input: "america/los_angeles"
Output: "PST"
Input: "Asia/Kolkata"
Output: "IST"
I know this can be done by mapping the timezone value to its corresponding abbreviation but it is time consuming.
So is there any PHP built-in function, though which I can achieve this?
Just use date('T') function to get the timezone. You'll need to set it though:
$input = 'america/los_angeles';
date_default_timezone_set($input);
echo date('T');
The reference is in the manual. T (capital) corresponds to Timezone abbreviation
Another variation would be to use DateTime classes:
$input = 'Asia/Kolkata';
$dt = new DateTime(null, new DateTimeZone($input));
echo $dt->format('T');
Edit: Take note, you'll also need to take into account if the input is correct or not, this throws an exception is its invalid.
You can just handle it via try/catch:
$input = 'Asia/Kolkata';
try {
$dt = new DateTime(null, new DateTimeZone($input));
echo $dt->format('T');
} catch(Exception $e) {
echo $e->getMessage();
}
From the php's documentation page:
$dateTime = new DateTime();
$dateTime->setTimeZone( new DateTimeZone( 'America/Los_Angeles' ) );
return $dateTime->format( 'T' );
Will return PST
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
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?
I am unable to set timezone for my DateTime objects.
Here is my code :
$dt = DateTime::createFromFormat('U',time(),new DateTimeZone('Asia/Kolkata'));
print_r($dt->getTimeZone());
Here is the output :
DateTimeZone Object
(
)
I also tried putting these lines at the top (one at a time) - without any success:
date_default_timezone_set('Asia/Calcutta');
ini_set('date.timezone', 'Asia/Calcutta');
date_default_timezone_set('Asia/Kolkata');
ini_set('date.timezone', 'Asia/Kolkata');
This is because you specified UNIX timestamp in the parameter. See what php manual says.
The timezone parameter and the current timezone are ignored when the time parameter either contains a UNIX timestamp (e.g. 946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00).
What you are trying to do can be easily done by,
$dt = new DateTime("now", new DateTimeZone('Asia/Kolkata'));
If you have a variable that contains UNIX timestamp, first create a DateTime object with it. Then set the new TimeZone.
$dt = new DateTime("#$timestamp");
$dt->setTimezone( new DateTimeZone('Asia/Kolkata'));
http://codepad.viper-7.com/topBCR
try this
<?php
$dateTimeZoneAsia = new DateTimeZone("Asia/Kolkata");
$dateTimeAsia = new DateTime("now", $dateTimeZoneAsia);
$timeOffset = $dateTimeZoneAsia->getOffset($dateTimeAsia);
var_dump($timeOffset);
?>