PHP Date Exercises : Print: Saturday the 7th
9. Display Date in "Saturday the 7th" Format
Write a PHP script to print like : Saturday the 7th
Sample Solution:
PHP Code:
<?php
echo date('l \t\h\e jS'); // Outputs the current day of the week followed by the day of the month with the suffix.
?>
Output:
Tuesday the 11th
Explanation:
In the exercise above,
- date('l \t\h\e jS'): Formats the current date and time according to the provided format string.
- l: Represents the full name of the day of the week (e.g., Monday, Tuesday, etc.).
- \t\h\e: Outputs the literal text "the" as part of the string.
- jS: Represents the day of the month with the suffix (e.g., 1st, 2nd, 3rd, etc.).
- echo: Outputs the formatted date string to the screen.
For more Practice: Solve these Related Problems:
- Write a PHP script to format the current day into "Day the nth" format, correctly calculating ordinal suffixes.
- Write a PHP function that converts a given date to a string showing the day of the week along with its ordinal date, e.g., "Wednesday the 21st".
- Write a PHP program that accepts a date and outputs it in the "Day the nth" format, ensuring proper suffix handling for 1st, 2nd, 3rd, etc.
- Write a PHP script to display today's date as "Day the nth" while handling edge cases such as special numbers (11th, 12th, 13th).
Go to:
PREV : First and Last Day of Month.
NEXT : Validate Given 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.