Listing Upcoming Events
Part of the Logic & Flow section of Coddy's PHP journey — lesson 61 of 68.
Challenge
EasyYou will receive two inputs: a JSON string representing the existing events array, and a date string in YYYY-MM-DD format representing today's date.
The first input will be a JSON string in this format: [{"name":"Team Meeting","date":"2024-04-15","location":"Office"},{"name":"Conference","date":"2024-05-20","location":"Convention Center"},{"name":"Workshop","date":"2024-03-10","location":"Library"}]
The second input will be a date string in this format: 2024-04-01
Read both inputs and decode the JSON string into an events array. Create two DateTime objects: one for today's date (using the second input) and one for each event's date as you iterate through the events.
Filter and display only the upcoming events - events whose date is greater than or equal to today's date. For each upcoming event, print it in the following format:
Upcoming: [name] on [date] at [location]
Print each upcoming event on a separate line, in the order they appear in the array. If an event's date is before today's date, do not print it.
Input format:
- First line: A JSON string representing the events array with keys
name,date, andlocation - Second line: A date string in YYYY-MM-DD format representing today's date (example:
2024-04-01)
Expected output: Each upcoming event printed on a separate line in the format: Upcoming: [name] on [date] at [location]
Try it yourself
<?php
// Read the JSON string of events
$eventsJson = fgets(STDIN);
$events = (array)json_decode($eventsJson, true);
// Read today's date
$todayDate = trim(fgets(STDIN));
// TODO: Write your code below
// Create DateTime object for today's date
// Loop through events and compare dates
// Print upcoming events in the required format
?>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 Exercise2Advanced 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 Sorting6Higher-Order Array Functions
Transforming with 'array_map''array_map' with Custom FuncFiltering with 'array_filter''array_filter' with Custom FunReducing with 'array_reduce'Walking with 'array_walk'Recap: Data Processing9Event Scheduler
Event DataAdding a New Event