w3resource

PHP Date Exercises : Display time in a specified time zone


28. Display Time in Specified Timezone

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

    For more Practice: Solve these Related Problems:

    • Write a PHP script to display the current time for a user-specified timezone, using DateTimeZone and DateTime objects.
    • Write a PHP function that accepts a timezone string and outputs the current date and time in that timezone, formatted appropriately.
    • Write a PHP program to convert the server time to multiple specified timezones and output each result in a table.
    • Write a PHP script to prompt the user for a timezone, then display the current time along with the timezone identifier.

    Go to:


    PREV : Days in Current Month.
    NEXT : PHP String Exercises Home.

    PHP Code Editor:



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

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.