Formatting DateTime Objects
Part of the Logic & Flow section of Coddy's PHP journey — lesson 49 of 68.
Now that you have a DateTime object, you need to convert it into a readable string format. The format() method is your tool for transforming a DateTime object into any date or time format you need for display purposes.
The format() method uses the exact same format characters you learned with the date() function. This consistency makes it easy to apply your existing knowledge to DateTime objects. You call the method directly on your DateTime object and pass the format string as a parameter.
<?php
$now = new DateTime();
echo $now->format("Y-m-d"); // Outputs: 2024-03-15
echo $now->format("H:i:s"); // Outputs: 14:30:25
echo $now->format("l, F j, Y"); // Outputs: Friday, March 15, 2024
?>The object-oriented approach with format() provides better code organization, especially when you're working with the same date multiple times. You create the DateTime object once and can format it in different ways throughout your code without repeatedly calling procedural functions.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
Challenge
EasyYou will receive two inputs: a date string in the format Y-m-d H:i:s and a format string. The first input is a specific date and time (example: 2024-03-15 14:30:25), and the second input is a format string that specifies how to display the date (example: l, F j, Y).
Read both inputs, create a DateTime object using the date string, use the format() method to transform the DateTime object according to the provided format string, and print the formatted result.
Input format:
- First line: A date string in the format
Y-m-d H:i:s(example:2024-03-15 14:30:25) - Second line: A format string (example:
l, F j, YorH:i:sorY-m-d)
Expected output: The date formatted according to the provided format string
Cheat sheet
The format() method converts a DateTime object into a readable string format using the same format characters as the date() function:
<?php
$now = new DateTime();
echo $now->format("Y-m-d"); // Outputs: 2024-03-15
echo $now->format("H:i:s"); // Outputs: 14:30:25
echo $now->format("l, F j, Y"); // Outputs: Friday, March 15, 2024
?>The object-oriented approach allows you to create a DateTime object once and format it multiple ways without repeatedly calling procedural functions.
Try it yourself
<?php
// Read the date string
$dateString = trim(fgets(STDIN));
// Read the format string
$formatString = trim(fgets(STDIN));
// TODO: Write your code below
// Create a DateTime object and format it according to the format string
// Output the formatted date
echo $formattedDate;
?>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Advanced Functions
Anonymous FunctionsClosures and 'use'Arrow FunctionsCallback FunctionsUsing 'call_user_func'Variable FunctionsPassing by ReferenceRecursive FunctionsRecap: Function Medley4Multi-dimensional Arrays
Creating a 2D ArrayAccessing 2D Array ElementsModifying 2D Array ElementsIterating with Nested Loops2D Associative ArraysRecap: Simple Grid Exercise7Working with Dates and Times
The 'date()' FunctionUnix Timestamps with 'time()'Intro to the DateTime ObjectFormatting DateTime ObjectsModifying DateTime ObjectsRecap: Date Calculations2Advanced Array Manipulations
Adding with 'array_push'Removing with 'array_pop'Adding with 'array_unshift'Removing with 'array_shift'Merging Indexed ArraysMerging Associative ArraysExtracting with 'array_slice'Values with 'in_array'Keys with 'array_search'Recap: Playlist Exercise3Sorting Arrays
Sort Indexed Arrays AscendingSort Indexed Arrays DescendingSort Assoc Arrays by ValueSort Assoc Arrays by KeyNatural Order SortingCustom Sorting with 'usort'Recap: Leaderboard Sorting