We now have a youtube channel. Subscribe!

PHP | timezone_name_get() Function

PHP timezone_name_get() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP timezone_name_get() Function.

The built-in timezone_name_get() function is an alias of the DateTimeZone::getName(). This php function accepts a DateTimeZone object as a parameter and then returns its timezone.

Syntax

Following below is the syntax to use this function -

timezone_name_get($object)


Parameter Details

Sr.NoParameter & Description
1

object (Mandatory)

This is a DateTimeZone object.


Return Value

This built-in function returns a string value specifying the timezone of the given object.

PHP Version

This function was first introduced as part of core PHP v 5.2.0 and, it works with all the later versions.

Example1

Below example shows the usage of the PHP timezone_name_get() function -

<?php
   //setting the timezone
   $tz = new DateTimeZone('Nigeria/Rivers');   
   $res = timezone_name_get($tz);
   print("Timezone: ".$res);
?>

Output

When the above code is executed, it will produce the following result -

Timezone: Nigeria/Rivers

Example2

Try the following example -

$dateSrc = '2007-04-19 12:50 GMT';
$dateTime = date_create( $dateSrc);
$DateTimeZone = timezone_open ( 'America/Chicago' );   
date_timezone_set( $dateTime, $DateTimeZone );
$NewDateTimeZone = date_timezone_get($dateTime);   
echo 'New timeZone is '. timezone_name_get($NewDateTimeZone);
echo "\n";
# Using second function.
$dateTime = new DateTime($dateSrc);
$DateTimeZone = timezone_open ( 'America/Chicago' );
$dateTime->setTimezone( $DateTimeZone );   
echo 'New timeZone is '. $DateTimeZone->getName ();

Output

When the above code is executed, it will produce the following result -

New timeZone is America/Chicago
New timeZone is America/Chicago


Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the timezone_offset_get() Function in PHP.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain