Menu
Coddy logo textTech

Recap: Date Calculations

Part of the Logic & Flow section of Coddy's PHP journey — lesson 51 of 68.

challenge icon

Challenge

Easy

You will receive three inputs: a start date, an end date, and a format string. The first two inputs are date strings in the format Y-m-d (example: 2024-03-15), and the third input is a modification expression like +5 days, -2 weeks, or +1 month.

Read all three inputs, create two DateTime objects from the start and end dates, apply the modification expression to the start date, and then determine which date comes later: the modified start date or the original end date. Print the later date in the format Y-m-d.

Input format:

  • First line: A start date string in the format Y-m-d (example: 2024-03-15)
  • Second line: An end date string in the format Y-m-d (example: 2024-04-10)
  • Third line: A modification string (example: +5 days or -2 weeks)

Expected output: The later date between the modified start date and the original end date, formatted as Y-m-d

Try it yourself

<?php
// Read the three inputs
$startDate = trim(fgets(STDIN));
$endDate = trim(fgets(STDIN));
$modification = trim(fgets(STDIN));

// TODO: Write your code below
// Create DateTime objects from the start and end dates
// Apply the modification to the start date
// Compare the modified start date with the end date
// Determine which date is later

// Output the later date in Y-m-d format
echo $laterDate;
?>

All lessons in Logic & Flow