Skip to Content
Course content

53: Working with Dates: DateTime and DateInterval

Click on the "Edit" button in the top corner of the screen to edit your slide content.

If you've been using the basic date() and strtotime() functions, you've probably realized they're fine for simple timestamps, but they quickly become a nightmare when you're doing actual "date math." That's where the DateTime class and DateInterval come in. They treat dates as objects rather than strings, which makes your life much easier.

To show you how this works in a real scenario, let's build a small piece of logic for a subscription service. We need to take a user's sign-up date, calculate their next billing date (one month later), and figure out exactly how many days are left until that happens.

Pinpointing the current moment

First, I'll start by creating a DateTime object. I'm not going to hardcode a date because I want to see it work in real-time. By calling the constructor without arguments, PHP assumes "now."

<?php
$signupDate = new DateTime(); 
echo "You signed up on: " . $signupDate->format('Y-m-d H:i:s') . "\n";
?>

I prefer using format() over the old date() function because the object carries its own state. It's cleaner and more portable.

Calculating the next bill date

Now, I need to move that date forward by one month. This is where DateInterval comes in. This class represents a fixed amount of time (like "one month" or "three days") rather than a specific point on a calendar.

<?php
// I'll try to create an interval for one month
$interval = new DateInterval('1 month'); 
$signupDate->add($interval);

echo "Next billing date: " . $signupDate->format('Y-m-d');
?>

Wait—I just hit a fatal error. I did this in my first few years of PHP too. I instinctively passed "1 month" into the DateInterval constructor. But DateInterval doesn't take human-readable English; it requires an ISO 8601 duration string. The "P" stands for Period, and the "M" stands for Months.

Let me fix that:

<?php
// Correcting the interval string to ISO 8601 format
$interval = new DateInterval('P1M'); 
$signupDate->add($interval);

echo "Next billing date: " . $signupDate->format('Y-m-d');
?>

Measuring the gap between dates

Adding time is great, but often you need to go the other way: finding the difference between two dates. For this, we use the diff() method. This method actually returns a DateInterval object, which is pretty convenient.

Let's say we want to tell the user exactly how many days they have left. I'll create a second object for "today" so I can compare it against the billing date we just calculated.

<?php
$today = new DateTime();
$billingDate = new DateTime();
$billingDate->add(new DateInterval('P1M'));

$difference = $today->diff($billingDate);

echo "Your next bill is in " . $difference->days . " days.";
?>

One thing to keep in mind: $difference->days gives you the total number of days. If you wanted the breakdown (like "1 month and 2 days"), you'd access properties like $difference->m or $difference->d. For a countdown, days is almost always what you actually want.

  • DateTime: A specific point in time.
  • DateInterval: A duration of time.
  • diff(): A way to find the distance between two points.



📋 Practical Task

Build a Trial Expiration Countdown

You are building a "Trial Period" feature for a software app. A user signs up for a 14-day free trial.

Write a PHP script that does the following:

  • Creates a DateTime object representing the user's sign-up date (you can use "now").
  • Uses DateInterval to calculate the expiration date exactly 14 days after sign-up.
  • Creates a second DateTime object representing "today" (simulating a check that happens a few days into the trial). To make this realistic, manually set this "today" date to be 5 days after the sign-up date.
  • Calculates the difference between "today" and the expiration date.
  • Prints a message: "Your trial expires in X days." (where X is the calculated difference).
Rating
0 0

There are no comments for now.

to be the first to leave a comment.