PHP Date Exercises : Print the current date in the specified format
3. Current Date Formatting
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 :

For more Practice: Solve these Related Problems:
- Write a PHP script that displays the current date in three different custom formats using the date() function.
- Write a PHP function to output today's date in a verbose format (e.g., "Monday, September 01, 2013") and a compact format simultaneously.
- Write a PHP program to print the current date and time in multiple international formats based on a parameter.
- Write a PHP script to generate a formatted date string that includes the day of the week, date, month, and year, using the date() function creatively.
Go to:
PREV : Birthday Countdown.
NEXT : Difference Between Two Dates.
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.