Home >>PHP Date Time Functions >PHP date_timezone_set() Function

PHP date_timezone_set() Function

PHP date_timezone_set() Function

PHP date_timezone_set() function is used to sets the time zone for the given DateTime object. It returns the DateTime object on success or False on failure. It accepts two parameters $object and the $timezoe.

Syntax:

 date_timezone_set(object, timezone);

Parameter Values

Parameter Description
object This is a required parameter. This parameter defines the DateTime object returned by date_create().
timezone This is a required parameter. The parameter defines the DateTimeZone object that represents the desired time zone.

Here is an example of date_timezone_set() function in PHP:

<html>
<body>
<?php
$date=date_create(); 
echo date_format($date,"d-M-Y H:i:s P")."<br>";  // current UTC
date_timezone_set($date,timezone_open("Asia/kolkata"));
echo date_format($date,"d-M-Y H:i:s P");  // india time
?>
</body>
</html>
Output:
01-Feb-2020 06:42:01 +00:00
01-Feb-2020 12:12:01 +05:30

Example 2:

<html>
<body>
<?php
$date=date_create("14-02-2020",timezone_open("Asia/kolkata")); 
echo date_format($date,"d-M-Y H:i:s P")."<br>";  // actual timezone
date_timezone_set($date,timezone_open("Europe/Paris"));
echo date_format($date,"d-M-Y H:i:s P");  // changed timezone
?>
</body>
</html>
Output:
14-Feb-2020 00:00:00 +05:30
13-Feb-2020 19:30:00 +01:00

No Sidebar ads