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
97: strtotime and Date Parsing
Imagine you have a personal assistant. You don't tell them, "Please schedule a meeting for 1,715,432,400 seconds after the Unix Epoch." That would be insane. Instead, you say, "Schedule it for next Friday at 3 PM." Your assistant hears that natural language, does the mental math, and puts a specific, concrete date on the calendar.
That is exactly what strtotime() does for your PHP code. It takes a human-readable string—something you'd actually say out loud—and converts it into a Unix timestamp (a big integer representing seconds). Once you have that integer, you can format it, compare it to other dates, or store it in a database.
Talking to PHP in English
The real magic here is that strtotime() is surprisingly flexible. I've spent years using this because it saves me from writing complex date-calculation loops. You can pass it almost any relative time description.
// The basics
echo strtotime("now");
echo strtotime("tomorrow");
echo strtotime("yesterday");
// Getting specific
echo strtotime("next Thursday");
echo strtotime("last Monday");
echo strtotime("first day of next month");
In a real app, you'll rarely just echo the timestamp. You'll usually wrap it in the date() function to make it readable again. For instance, if you're building a "Your trial expires on..." message, you don't want to show the user a 10-digit number; you want a formatted date.
$trialStart = "2023-10-01";
$expiryDate = strtotime($trialStart . " + 14 days");
echo "Your trial ends on " . date("F j, Y", $expiryDate);
// Result: Your trial ends on October 15, 2023
Doing Date Math on the Fly
One thing I love about this function is how it handles relative modifiers. You can chain them or use specific intervals. This is incredibly useful for things like generating weekly reports or calculating billing cycles.
Check out how we can manipulate the string to get exactly what we need:
// What's the date two weeks from now?
$twoWeeksHence = strtotime("+2 weeks");
// What was the date 3 months ago?
$threeMonthsAgo = strtotime("-3 months");
// I need the end of the current month for a payroll deadline
$payrollDeadline = strtotime("last day of this month");
Just a heads-up: be careful with "month" math. Adding "1 month" to January 31st can sometimes give you March 3rd (because February doesn't have 31 days). It's a quirky behavior of the underlying logic. If you're doing high-precision financial calculations, you'll eventually want to move toward the DateTime class, but for 90% of tasks, strtotime() is the fastest way to get the job done.
When PHP Gets Confused
Since strtotime() is trying to "guess" what you mean, it can fail. If you pass it a string it doesn't recognize—like "last Bluesday"—it won't throw an error; it will simply return false.
I always recommend checking the result if the input string is coming from a user or an external API. If you don't, and you pass false into the date() function, PHP will treat it as 0, and your app will suddenly claim that everything happened on January 1st, 1970. That's a classic bug that's easy to miss if you aren't looking for it.
$userInput = "some weird date";
$timestamp = strtotime($userInput);
if ($timestamp === false) {
echo "Sorry, I couldn't understand that date!";
} else {
echo "Date parsed successfully!";
}
📋 Practical Task
Exercise: Subscription Renewal Date Calculator
You are building a subscription management system. Your goal is to create a script that calculates the next billing date based on a user's sign-up date and their chosen plan.
Requirements:
- Create a variable
$signupDatewith a value of"2024-05-12". - Create a variable
$planTypewith a value of either"monthly"or"yearly". - Using a conditional (if/else) and
strtotime(), calculate the$renewalDate:- If the plan is "monthly", the renewal is 1 month after the signup date.
- If the plan is "yearly", the renewal is 1 year after the signup date.
- Print the final result in a friendly sentence using
date(), formatted as: "Your next billing date is: [Month] [Day], [Year]" (e.g., June 12, 2024).
There are no comments for now.