w3resource

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

PHP date: Exercise-14 with Solution

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'

PHP Code Editor:

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

Previous: Write a PHP script to get yesterday's date.
Next: Write a PHP script to check if a date is a weekend or not.

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-14.php