w3resource

PHP Date Exercises : Get the current date/time of 'Australia/Melbourne'


14. Current Date/Time for Australia/Melbourne

Write a PHP script to get the current date/time of 'Australia/Melbourne'.

Sample Solution:

PHP Code:

<?php
date_default_timezone_set('America/Los_Angeles');
$date = date('m/d/Y h:i:s a', time());
echo $date;
?>

Output:

07/11/2017 12:52:31 am 

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

Explanation:

In the exercise above,

  • date_default_timezone_set('America/Los_Angeles');: Setting the default timezone to 'America/Los_Angeles' to ensure that the date and time are displayed in the Pacific Time Zone.
  • $date = date('m/d/Y h:i:s a', time());: Get the current date and time using the date() function with the specified format 'm/d/Y h:i:s a', where:
    • 'm' represents the month (01 to 12)
    • 'd' represents the day of the month (01 to 31)
    • 'Y' represents the year (e.g., 2017)
    • 'h' represents the hour in 12-hour format (01 to 12)
    • 'i' represents the minutes (00 to 59)
    • 's' represents seconds (00 to 59)
    • 'a' represents lowercase Ante meridiem and Post meridiem (am or pm)
  • echo $date: Outputs the formatted date and time.

Flowchart :

Flowchart: Get the current date/time of 'Australia/Melbourne'

For more Practice: Solve these Related Problems:

  • Write a PHP script to set the default timezone to 'Australia/Melbourne' and then display the current date and time in a formatted string.
  • Write a PHP function to change the timezone to 'Australia/Melbourne' and output a custom greeting with the current local time.
  • Write a PHP program to compare the server time with 'Australia/Melbourne' time and display both.
  • Write a PHP script to retrieve and display the current date and time for Australia/Melbourne using DateTimeZone and DateTime objects.

Go to:


PREV : Get Yesterday's Date.
NEXT : Check if Date is a Weekend.

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.