How do I get my local timezone name in php? [duplicate] - php

This question already has answers here:
Get name of time zone
(14 answers)
Closed 2 years ago.
I need my local timezone name like America/Chicago ?
I have written my code like this
$date = new DateTime();
$timeZone = $date->getTimezone();
echo $timeZone->getName();
But I am getting UTC as output but I don't need this. I need the timeZone name. How can I get it ?

You are probably getting the timezone set in the server, that's the best you can get. If you want America/Chicago you will need to configure that in the server.
You could also specify the timezone you want to work with when instantiating a new DateTime object, but since the server is probably set as UTC, you need to be careful and juggle the conversion. For instance:
$dt = new DateTime('2014-06-22 11:10:58', new DateTimeZone('UTC'));
$dt->setTimezone(new DateTimeZone("Europe/Paris"));
$dateString = $dt->format('Y-m-d h:i:s');

Related

How can I convert time to UTC in PHP? [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 2 years ago.
I am getting time with timezone from an API.
Example: 2019-06-25T08:01:32-05:00
How can I change to UTC time using PHP?
You can use the DateTime and DateTimeZone classes:
// this date format will consider the timezone '-05:00'
$dt = new DateTime('2019-06-25T08:01:32-05:00');
// then you convert it to utc
$dt->setTimeZone(new DateTimeZone('utc'));
echo $dt->format('Y-m-d H:i:s'); // will give you 2019-06-25 13:01:32
Manual DateTime: https://www.php.net/manual/en/class.datetime.php
Manual DateTimeZone: https://www.php.net/manual/en/class.datetimezone.php

Why setting date timezone by config() doesn't affect on date() result in Laravel [duplicate]

This question already has an answer here:
How can I change the timezone of the outputted date in Laravel 4?
(1 answer)
Closed 4 years ago.
The default timezone that is set in config/app file is UTC but for a short part of my project I want to change it to Asia/Tehran. I use config() function to set timezone value to what I want but it doesn't affecton date result.
please look to the below example:
echo date('Y-m-d H:i:s',time()) . '<br>'; //here the current timezone is UTC
config(['app.timezone' => 'Asia/Tehran']); // I've set new timezone
echo date('Y-m-d H:i:s',time()); //here the current timezone is Asia/Tehran
The above code result is same but they should be different.
Where is the problem?
custom timezone config doesn't affect outputted date, try this:
date_default_timezone_set('Asia/Tehran');
echo date('Y-m-d H:i:s',time());
I think that config changed at runtime will not take effect by the way why not to use Carbon
$timestamp = '2018-02-08 18:39:00';
$date = Carbon::createFromFormat('Y-m-d H:i:s', $timestamp, 'Europe/Stockholm');
$date->setTimezone('UTC');

Convert TIMESTAMP and timezone to a different timezone [duplicate]

This question already has answers here:
Timezone conversion in php
(9 answers)
Closed 6 years ago.
So I have in the DB a timestamp like this: 2016-03-31 21:10:15 this get's set in db to the server timezonedate_default_timezone_set("America/Los_Angeles");
Now when I list the data I need to convert this to a user timezone which I have stored somewhere else, is there a way to convert this and display it in a different timezone?
So if it was 5:00 PM for "America/Los_Angeles" I need to convert this to New_York for eg and show that time but for New_York timezone, I guess you know what I mean.
Use DateTime objects, and you can play around with timezones to your heart's content:
$dateTime = '2016-03-31 21:10:15';
$originalTimezone = 'America/Los_Angeles';
$newTimezone = 'America/New_York';
$dto = new DateTime($dateTime, new DateTimeZone($originalTimezone));
$dto->setTimezone(new DateTimeZone($newTimezone));
echo $dto->format('Y-m-d H:i:s');

How to convert timestamp to actual Date and Time with GMT timezone [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
I have a timestamp which is like this
$timestamp = time();
so the $timestamp has 10 digit timestamp value...
now i use the follwoing code to convert it into (YYYY-mm-dd H:i) format by the following method
$act_time = date('Y-m-d H:i', $arr_timestamp);
But now i want to convert it based on the GMT timezone format..
How can i achieve that?
NOTE: I dont want to use php's DateTime object
You can use date_default_timezone_set():
date_default_timezone_set("GMT");
List of Supported Timezones.
You can save the current timezone before changing it:
$backupTimezone = date_default_timezone_get();
In addition, instead of using these you can change the default timezone in your ini file: date.timezone

Reformat datetime variable in PHP [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
I have the following datetime 2014-02-05 17:12:48 stored in a php variable named $reportDb["reportDate"].
Now, It is in Y-m-d H:i:s format, I want it in d/m/Y H:i:s format. How can I set or reformat this variable?
I tried with datetime::createformat but It doesn't works.
$formatted = date("d/m/Y H:i:s", strtotime($reportDb["reportDate"]))
If you want another method. Even if i would prefer the one already mentioned.
Output 05/02/2014 17:12:48
PHP 5.3 and older:
$dt = new DateTime('2014-02-05 17:12:48');
echo $dt->format(''d/m/Y H:i:s'');
PHP 5.4+:
$dt = (new DateTime('2014-02-05 17:12:48'))->format(''d/m/Y H:i:s'');
with the variable:
$dt = (new DateTime($reportDb["reportDate"]))->format(''d/m/Y H:i:s'');
If I understand you right, $reportDb["reportDate"] is already a DateTime object.
If so, all you need is ...
$reportDb["reportDate"]->format('d/m/Y H:i:s');
Cheers!

Categories