PHP Date Exercises : Display time in a specified time zone
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](https://www.w3resource.com/w3r_images/php-date-exercise-28.png)
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.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics