LargeTuna
LargeTuna

Reputation: 2824

PHP Display Date Time with Offset

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

Upvotes: 1

Views: 1258

Answers (3)

Sandeep K.
Sandeep K.

Reputation: 759

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

Upvotes: 1

user8554313
user8554313

Reputation:

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

Upvotes: 1

John Conde
John Conde

Reputation: 219804

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

Upvotes: 1

Related Questions