w3resource

PHP Date Exercises : Display time in a specified time zone

PHP date: Exercise-28 with Solution

Write a PHP script to display time in a specified timezone.

Sample Solution:

PHP Code:

<?php
// Set the default timezone to 'America/New_York'
ini_set('date.timezone','America/New_York'); 
// Output the current time in 12-hour format with AM/PM indication
echo '<p>'.date("g:i A").'</p>'."\n";
?>

Output:

7:39 AM 

N.B.: The result may varry for your system date and time.

Explanation:

In the exercise above,

  • ini_set('date.timezone','America/New_York');: Sets the default timezone to 'America/New_York'.
  • echo '<p>'.date("g:i A").'</p>'."\n";: Outputs the current time in 12-hour format (g:i A) with AM/PM indication wrapped in HTML paragraph (<p>) tags.

  • Flowchart :

    Flowchart: Display time in a specified time zone

    PHP Code Editor:

    Have another way to solve this solution? Contribute your code (and comments) through Disqus.

    Previous: Write a PHP script to get the number of days of the current month.
    Next: PHP String Exercises Home.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/php-exercises/php-date-exercise-28.php