Get unix timestamp from string without setting default timezone in PHP


String has a time zone, and there is usually no need to set a default time zone. But when it needs to be printed, the default time zone can be set explicitly. Below is the code to do the same −

Default timezone

Example

 Live Demo

echo date_default_timezone_get();

Output

This will produce the following output −

UTC

When timezone is specified

Example

 Live Demo

echo date("Y-m-d H:i:s",strtotime("1/1/2020 00:00:00 America/Los_Angeles"));

Output

This will produce the following output −

2020-01-01 08:00:00

Another way of specifying timezone

Example

 Live Demo

echo date("Y-m-d H:i:s",strtotime("1/1/2020 00:00:00"));

Output

This will produce the following output −

2020-01-01 00:00:00

Updated on: 06-Apr-2020

464 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements