Modifying DateTime Objects
Part of the Logic & Flow section of Coddy's PHP journey — lesson 50 of 68.
Once you have a DateTime object, you often need to change it to represent a different moment in time. The modify() method allows you to add or subtract time from your DateTime object using natural language expressions, making date calculations intuitive and readable.
The modify() method accepts the same types of strings that work with strtotime(), such as '+1 day', '-2 weeks', or 'next Friday'. Unlike creating a new object each time, this method modifies the existing DateTime object directly, which is efficient for sequential date operations.
<?php
$date = new DateTime();
echo $date->format('Y-m-d'); // Today's date
$date->modify('+1 week');
echo $date->format('Y-m-d'); // One week from today
$date->modify('-3 days');
echo $date->format('Y-m-d'); // 4 days from today
?>This approach gives you powerful flexibility for date calculations. You can chain multiple modifications or use complex expressions like '+2 months -1 day' to perform sophisticated date arithmetic while keeping your code clear and maintainable.
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 modification string. The first input is a specific date and time (example: 2024-03-15 14:30:25), and the second input is a modification expression like +1 week, -3 days, or +2 months.
Read both inputs, create a DateTime object using the date string, use the modify() method to apply the modification expression to the DateTime object, and print the modified date in the format Y-m-d H:i:s.
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 modification string (example:
+1 weekor-3 daysor+2 months)
Expected output: The modified date formatted as Y-m-d H:i:s
Cheat sheet
The modify() method allows you to add or subtract time from a DateTime object using natural language expressions:
<?php
$date = new DateTime();
$date->modify('+1 week'); // Add one week
$date->modify('-3 days'); // Subtract three days
$date->modify('+2 months -1 day'); // Complex expressions
?>The method modifies the existing DateTime object directly and accepts the same string formats as strtotime(), such as '+1 day', '-2 weeks', or 'next Friday'.
Try it yourself
<?php
// Read the date string
$dateString = trim(fgets(STDIN));
// Read the modification string
$modificationString = trim(fgets(STDIN));
// TODO: Write your code below
// Create a DateTime object, apply the modification, and format the result
// Output the modified date
echo $modifiedDate;
?>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