Intro to the DateTime Object
Part of the Logic & Flow section of Coddy's PHP journey — lesson 48 of 68.
While the procedural functions like date(), time(), and strtotime() work well for basic date operations, PHP offers a more powerful and flexible approach through the DateTime class. This object-oriented method provides better organization, clearer syntax, and more advanced functionality for working with dates and times.
The DateTime class represents a specific moment in time as an object, which you can then manipulate and format using various methods. Unlike procedural functions that work with separate timestamps and format strings, a DateTime object encapsulates both the date/time data and the operations you can perform on it.
<?php
$now = new DateTime();
echo $now; // Outputs something like: 2024-03-15 14:30:25
?>Creating a new DateTime object without any parameters automatically sets it to the current date and time.
The object-oriented approach makes your code more readable and maintainable, especially when you need to perform multiple operations on the same date. You'll find this particularly useful as you work with more complex date calculations and formatting requirements.
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 one input: a date string in the format Y-m-d H:i:s (example: 2024-03-15 14:30:25).
Read the input date string, create a DateTime object using this date string as a parameter, and print the DateTime object directly.
When you create a DateTime object with a date string parameter, it represents that specific moment in time instead of the current date and time. Simply echo the object to display it.
Input format: One line containing a date string in the format Y-m-d H:i:s (example: 2024-03-15 14:30:25)
Expected output: The DateTime object displayed in its default format
Cheat sheet
The DateTime class provides an object-oriented approach to working with dates and times in PHP, offering better organization and flexibility than procedural functions.
Create a DateTime object for current time:
$now = new DateTime();Create a DateTime object for a specific date:
$specificDate = new DateTime("2023-05-15");Use the format() method to display dates in specific formats:
echo $now->format("Y-m-d H:i:s"); // 2024-03-15 14:30:25
echo $specificDate->format("Y-m-d"); // 2023-05-15Try it yourself
<?php
// Read the date string input
$dateString = trim(fgets(STDIN));
// TODO: Create a DateTime object using the date string and print it
?>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