CodeBlanks Logo Icon CodeBlanks

Challenge - Mini API

PHP course · Intermediate · Lesson 115/115

Build a simple task tracking API with classes, JSON and DateTime.

Challenge - Mini API

In this last challenge you'll build a simple task tracking API. The Task class stores a title, a done flag and a creation time; its summarize() method turns them into a one-line report.

The creation time uses the DateTime class; format("Y-m-d H:i") produces a custom date string. A task is an object, a collection is an associative array. This combines your class, DateTime and array knowledge.

Task class skeleton

The Task class holds three private properties: private string $title, private bool $done = false, private DateTime $createdAt. The constructor takes the title and DateTime; $done defaults to false.

The markDone() method sets $this->done = true. The summarize(): string method returns the task in the form "[x] Title (2026-01-15 10:00)"; an unfinished task shows a space inside the brackets. The $this->createdAt->format("Y-m-d H:i") call formats the date.

Exercise Write the Task class and summarize method

Define a Task class with private string $title, private bool $done = false, private DateTime $createdAt. The constructor takes string $title and DateTime $createdAt. The markDone(): void method sets $this->done = true. The summarize(): string method starts with "[x] " when $this->done is true and "[ ] " otherwise; it then appends $this->title and " (" . $this->createdAt->format("Y-m-d H:i") . ")". Add the call $t = new Task("Fix login bug", new DateTime("2026-01-15 10:00")); $t->markDone(); echo $t->summarize() . "\n";.

Show example solution
i').
Example solution:
<?php
// Write the Task class and summarize method
class Task {
    private string $title;
    private bool $done = false;
    private DateTime $createdAt;

    public function __construct(string $title, DateTime $createdAt) {
        $this->title = $title;
        $this->createdAt = $createdAt;
    }

    public function markDone(): void {
        $this->done = true;
    }

    public function summarize(): string {
        $mark = $this->done ? "[x]" : "[ ]";
        return $mark . " " . $this->title . " (" . $this->createdAt->format("Y-m-d H:i") . ")";
    }
}

$t = new Task("Fix login bug", new DateTime("2026-01-15 10:00"));
$t->markDone();
echo $t->summarize() . "\n";

Test yourself

  1. What does format('Y-m-d') return for new DateTime('2026-01-15 10:00')?

    • 15-01-2026
    • 2026-01-15
    • 10:00
    • 2026-01-15 10:00
    Show answer

    2026-01-15 — Correct. Y gives the year, m the zero-padded month, d the zero-padded day; the result is '2026-01-15'.

  2. Complete the format call

    $date = $this->createdAt->format(____);
    Show answer
    $date = $this->createdAt->format("Y-m-d H:i");
  3. Match each function to its job

    new DateTime
    creates date object
    format
    formats date as string
    markDone
    marks task done
    summarize
    produces report

All lessons in Intermediate