w3resource

PHP Date Exercises : Print the current date in the specified format

PHP date: Exercise-3 with Solution

Write a PHP script to print the current date in the following format. To get current date's information you can use the date() function.

Sample format : (assume current date is September 01, 2013)
2013/09/01
13.09.01
01-09-13

Sample Solution:

PHP Code:

<?php
// Print the current date in the format Year/Month/Day using date() function with "Y/m/d" format
echo date("Y/m/d") . "\n";

// Print the current date in the format Year.Month.Day using date() function with "y.m.d" format
echo date("y.m.d") . "\n";

// Print the current date in the format Day-Month-Year using date() function with "d-m-y" format
echo date("d-m-y")."\n";
?>

Output:

2017/02/16                                                  
17.02.16                                                    
16-02-17

Explanation:

In the exercise above,

  • echo date("Y/m/d") . "\n";: Prints the current date in the format Year/Month/Day (e.g., 2024/03/06).
  • echo date("y.m.d") . "\n";: Prints the current date in the format Year.Month.Day (e.g., 24.03.06).
  • echo date("d-m-y")."\n";: Prints the current date in the format Day-Month-Year (e.g., 06-03-24).

Flowchart :

Flowchart: Print the current date in the specified format

PHP Code Editor:

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

Previous: Create a simple 'birthday countdown' script, the script will count the number of days between current day and birthday.
Next: Write a PHP script to calculate the difference between two dates.

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://www.w3resource.com/php-exercises/php-date-exercise-3.php