Menu
Coddy logo textTech

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.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

challenge icon

Challenge

Easy

You 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, Y or H:i:s or Y-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;
?>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow