PHP
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions
-
Section 4: Object-Oriented PHP
-
Section 5: Working with Data
-
Section 6: Modern PHP (PHP 8)
-
Section 7: Working with Files and Networking
-
Section 8: Common PHP Frameworks Overview
-
Section 9: Tooling and Ecosystem
-
Section 10: Practical Projects
-
Section 11: Interview Practice
-
Section 12: More Standard Library
-
Section 13: Data Structures and Algorithms in PHP
-
Section 14: More Practice Exercises
-
Section 15: More Security and Best Practices
-
Section 16: More Testing and Tooling
-
Section 17: WordPress-Style CMS Concepts
-
Section 18: Advanced OOP Practice
-
Section 19: More Web Fundamentals
-
Section 20: Database Practice
-
Section 21: PHP Manual: Array Functions
-
Section 22: PHP Manual: Date and Calendar Functions
-
Section 23: PHP Manual: Filesystem and Directory Functions
-
Section 24: PHP Manual: Filter and Var Handling
-
Section 25: PHP Manual: Math Functions
-
Section 26: PHP Manual: JSON and XML
-
Section 27: PHP Manual: Network and Stream Functions
-
Section 28: PHP Manual: Error and Exception Handling
-
Section 29: PHP Manual: Output Control and Misc
-
Section 30: PHP Manual: FTP, Zip, and Mail
-
Section 31: Modern PHP Frameworks Deep Dive
-
Section 32: PHP Design Patterns
-
Section 33: More Practice Exercises
-
Section 34: PHP Performance and Deployment
-
Section 35: More Interview Practice
-
Section 36: More PHP Standard Library
-
Section 37: PHP Concurrency and Async
-
Section 38: More Web Development Practice
-
Section 39: PHP Testing Deep Dive
-
Section 40: Composer and Package Development
-
Section 41: PHP Security Deep Dive
-
Section 42: More Practical Projects
-
Section 43: Legacy PHP Maintenance
-
Section 44: More Algorithm Practice
-
Section 45: Final Practice and Review
-
Section 46: PHP for E-Commerce Patterns
-
Section 47: PHP API Design Deep Dive
-
Section 48: PHP Caching Strategies
-
Section 49: PHP Queue and Background Jobs
-
Section 50: PHP Multi-Tenancy Patterns
-
Section 51: PHP Real-Time Features
-
Section 52: PHP CMS and Content Modeling
-
Section 53: PHP Internationalization
-
Section 54: More Framework-Specific Practice
-
Section 55: PHP Legacy Code Refactoring
-
Section 56: More Practice Projects Round 2
-
Section 57: PHP Command-Line Applications
-
Section 58: PHP and Microservices
-
Section 59: More Interview and Review Round 2
96: DateInterval and DatePeriod
How do I define a specific duration of time without a fixed date?
You've probably spent a lot of time with DateTime, but DateTime is a point in time. When you want to describe a duration—like "three weeks" or "two months and five days"—that's where DateInterval comes in. It doesn't know what today is; it only knows how much time has passed or needs to pass.
The trickiest part about DateInterval is the constructor. It uses the ISO 8601 duration format, which looks like a secret code at first. It starts with 'P' (for Period), followed by the years, months, days, etc. Here is how I usually handle it:
// 'P1M2D' means Period: 1 Month and 2 Days
$interval = new DateInterval('P1M2D');
$now = new DateTime();
$futureDate = $now->add($interval);
echo "If you wait 1 month and 2 days from now, it will be: " . $futureDate->format('Y-m-d');
I'll be honest: memorizing those strings (P1Y, P1M, P1D) is a pain. If you find it confusing, you can also pass an associative array to the constructor using keys like 'days' or 'months', but the string format is what you'll see most often in professional codebases.
I have a start and end date; how do I get every day in between?
If you're building something like a booking calendar or a project timeline, you don't want to write a while loop and manually add days to a date object. That's tedious and error-prone. Instead, use DatePeriod.
DatePeriod is an iterator. It takes a start date, an interval, and an end date, and then lets you loop through the resulting dates as if they were a simple array. Check this out:
$start = new DateTime('2023-12-01');
$end = new DateTime('2023-12-10');
$interval = new DateInterval('P1D'); // One day intervals
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $date) {
echo $date->format('Y-m-d') . "\n";
}
Notice that the end date is exclusive. In the example above, December 10th won't actually show up in the loop. If you need the end date included, you'll have to add one day to your end date object before passing it into the DatePeriod constructor.
Can I use this for recurring events, like a monthly subscription?
Absolutely. One of the coolest things about DatePeriod is that you don't actually need an end date. You can tell it exactly how many times to repeat the interval instead.
This is perfect for things like generating a payment schedule for a customer. If a user signs up for a 6-month plan, you don't need to calculate the end date first; just tell PHP to repeat the "one month" interval six times.
$startDate = new DateTime('first day of this month');
$interval = new DateInterval('P1M'); // Monthly
$recurrences = 6;
// Note: we omit the end date and pass the number of recurrences instead
$period = new DatePeriod($startDate, $interval, $recurrences);
foreach ($period as $date) {
echo "Payment due on: " . $date->format('F j, Y') . "\n";
}
I prefer this approach for subscriptions because it avoids the "off-by-one" errors that often happen when calculating end dates manually.
📋 Practical Task
Exercise: Generating a 12-Month Membership Milestone Map
Imagine you are building a membership portal. When a user joins, you want to show them a list of the exact dates they will hit their 1-month, 2-month, ..., and 12-month milestones.
Your Task:
- Create a
DateTimeobject representing the user's join date (you can use any date from the past or present). - Create a
DateIntervalfor exactly one month. - Use
DatePeriodto generate a sequence of 12 dates starting from the join date. - Loop through the period and print each date in the format: "Milestone [X]: [Date]" (where [X] is the month number from 1 to 12).
There are no comments for now.