Recap: Date Calculations
Part of the Logic & Flow section of Coddy's PHP journey — lesson 51 of 68.
Challenge
EasyYou 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 daysor-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
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