PHP Display Date Time with Offset - php

In php how can I display the current date and time with the timezone offset such as this:
date_default_timezone_set('America/Denver');
$now = new \DateTime('NOW');
echo $now->format("Y-m-d h:i:s");
//
This displays 2019-01-09 19:30:19
but I want it to display 2019-01-09 07:00:00 because I am in Mountain Time Zone

When working with time zones and DateTime() objects you should be using DateTimeZone() instead of date_default_timezone_set()
$now = new \DateTime(null, new DateTimeZone('America/Denver'));
echo $now->format("Y-m-d h:i:s");
Demo

Try to paste second parameter your timezone
$date = new DateTime('NOW', new DateTimeZone('America/Denver));
echo $date->format('Y-m-d h:i:s');
You could define wich timezone is fitting for you by this answer.
List of US Time Zones for PHP to use?
for getting all timezones names in you country. Then you pick your timezone and paste
$date = new DateTime('NOW', new DateTimeZone('Your Country/Your timezone'));

you have right code, but you have want to show time in 12 hours format. then you have use something like that.
date_default_timezone_set('America/Denver');
$now = new \DateTime('NOW');
echo $now->format("Y-m-d h:i:s A");

Related

Convert UTC date&time to Europe/Lisbon

I want to convert date UTC for Europe/Lisbon, but the code I have gives me different outputs/times:
$datafull = "13-04-2021 08:47:13";
$date = new DateTime($datafull);
$date->setTimezone(new DateTimeZone('Europe/Lisbon'));
echo $date->format('d-m-Y H:i:s (e)');
// 13-04-2021 09:47:13 (Europe/Lisbon)
$datetime = new DateTime($datafull, new DateTimeZone('Europe/Lisbon'));
print $datetime->format('d-m-Y H:i:s (e)');
// 13-04-2021 08:47:13 (Europe/Lisbon)
When you supply a timezone object to the DateTime constructor you're telling it in what timezone the give $datafull is. So in:
$datetime = new DateTime($datafull, new DateTimeZone('Europe/Lisbon'));
You say it is in Europe/Lisbon, and it stays there.
In the other code:
$date = new DateTime($datafull);
$date->setTimezone(new DateTimeZone('Europe/Lisbon'));
The default timezone is used when the DateTime is constructed, probably UTC on your server, and then you change it afterwards on the second line to Europe/Lisbon, which is an hour ahead.
See: DateTime::__construct

How to convert stored database utc timestamps data into local timezone in PHP

is there any chance to convert database utc zone timestamps record into local timezone in PHP?
//example of code
$utc_date = $row['utc_date'];
// common values
$timezone = "Australia/Melbourne";
$tz = new DateTimeZone($timezone);
$dt = new DateTime();
$dt->setTimezone($tz);
// this can run inside a loop or by itself
$utc_date = $row['utc_date'];
$dt->setTimestamp(strtotime($utc_date));
$datetime = $dt->format("Y-m-d H:i:s");
// use the formatted date as required
echo $datetime;
You can create a new DateTime object inside a loop if you prefer or create it outside the loop and reuse it.
If the date/times don't come out of the database in the format 2019-07-18 10:00:00+00:00 you may have to add +00:00 to the retreived date.
You can simple use following exmaple to resolve your problem
<?php
//Conside in UTC the date and time will be looks like this
$utcTime = time();
echo date("Y-m-d H:i:s", $utcTime);
//now change it to your time zone
date_default_timezone_set('Your-local-time-zone');
$utc_date = date("Y-m-d H:i:s", strtotime($row['utc_date']));
?>
Let Say for my time zone I'll use
date_default_timezone_set('Asia/Kolkata');
Also, You can find your timezone supported by PHP or not here
You can read more about PHP Date funcation from here

Convert the UTC datetime to any other time zone dynamically in php

I want to convert the UTC datetime to any other time zone.its working fine.
But it showing only for one time zone,so to view the other time zone need to change the code every time.
i want to convert the UTC datetime to any other time zone dynamically.may be by selecting drop down or some other way.
<?php
$date = new DateTime('2017-11-15 01:00:00 +00');
$date->setTimezone(new DateTimeZone('Asia/Calcutta'));
echo $date->format('Y-m-d H:i:s');
?>
You can timezone by this code where you can change the $timezone variable value from your dropdown.
$timezone= 'Asia/Calcutta';
$date = new DateTime('2017-11-15 01:00:00 +00');
$date = new DateTime($date, new DateTimeZone($timezone));
echo $date->format('Y-m-d H:i:s');

How to get current date and time with respect to Time Zone in Codeigniter

When I run the following code in codeigniter, it returns incorrect time and date for the India location. Please let me the solution.
'date_default_timezone_set('Asia/Kolkata');
$date = date('Y-m-d H:i:s', time());
echo $date;'
try this
$now = new DateTime();
$now->setTimezone(new DateTimezone('Asia/Kolkata'));
echo $now->format('Y-m-d H:i:s');

How to return Date value

Consider this simple code:
$datetime = new DateTime('2015-12-12 23-23', new DateTimeZone('UTC'));
$timezone = new DateTimeZone('Europe/London');
$datetime->setTimezone($timezone);
Now I want to display the date in Europe/London time zone I've just applied but without giving any date format. I want the date format to be the same as input date.
This: $datetime->format() doesn't work. Cannot find other method than format() to get this date :/
you can simply use date not DateTime
<?php
date_default_timezone_set('Europe/London');
echo date('Y-m-d H-i');

Categories