Menu
Coddy logo textTech

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.

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 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 week or -3 days or +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;
?>
quiz iconTest yourself

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

All lessons in Logic & Flow