1

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

John Conde
  • 217,595
  • 99
  • 455
  • 496
LargeTuna
  • 2,694
  • 7
  • 46
  • 92

3 Answers3

1

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'));
NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
1

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

John Conde
  • 217,595
  • 99
  • 455
  • 496
1

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"); 
Sandeep K.
  • 759
  • 6
  • 18