Skip to Content
Course content

97: strtotime and Date Parsing

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

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 $signupDate with a value of "2024-05-12".
  • Create a variable $planType with 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).
Rating
0 0

There are no comments for now.

to be the first to leave a comment.