Calculating Monday's Timestamp in PHP: A Cleaner Approach
You want to find the timestamp for Monday of the current week, regardless of what day it is today. Your initial attempt used date("D",$x) where $x was a date string formatted as d-m-y. That works fine for getting the day name if you pass a valid Unix timestamp or a string that strtotime() can parse, but date("D", "21-02-10") fails because "21-02-10" is not a valid timestampâitâs just a formatted date string. PHP needs seconds since epoch, or something strtotime() can convert to one.
You then tried to loop through the next six days and manually check which one matches Monday. That logic works in principle but is unnecessarily complicated. Youâre doing heavy lifting with string parsing, manual day increments, and an array to store dates you donât actually need until the end. The core issue is that you're trying to solve a calendar problem using only raw date math instead of leveraging PHPâs built-in date functions.
Using strtotime() for Relative Dates
PHP has a powerful feature: relative date strings like "last Monday" or "this Monday". These are parsed by strtotime() into a Unix timestamp automatically. This is exactly what you need to get the start of the current week.
Here's how you can do it:
$mondayTimestamp = strtotime('Monday this week');
$mondayDate = date('d-m-Y', $mondayTimestamp);
echo $mondayDate;
This will always return the same Monday for all days in the current week. If today is Thursday, it returns last Monday? Noâ'Monday this week' means the upcoming Monday if today is before Monday, or last Monday if today is after Monday. Waitâactually, 'this week' in PHPâs strtotime() context refers to the ISO calendar week (MondayâSunday). So 'this Monday' gives you the first Monday of the current ISO week. Thatâs exactly what you want.
If you want to be even more explicit and avoid any ambiguity about which Monday is meant, use:
$mondayTimestamp = strtotime('next Monday');
$mondayDate = date('d-m-Y', $mondayTimestamp);
echo $mondayDate;
'next Monday' always gives you the upcoming Monday, even if today is already past Monday. If you prefer to get the Monday that started the current week (even if it's in the past), use 'this Monday'.
Why Your Original Approach Was Overcomplicated
Your code manually calculates dates by adding days to the day component:
$startdate2 = date('d-m-Y', mktime(0,0,0,$parts[1],($parts[0]+1),$parts[2]));
This is fragile. It assumes you know how many days each weekday is from today, and it requires storing intermediate results in an array just to find the one that matches Monday. Youâre doing string manipulation (explode('-')) when PHPâs date functions already handle that internally.
Also, mktime() expects a month (1â12), day, year, etc., so you have to split your own date string into those components. Thatâs extra work for no benefit. The strtotime() approach bypasses all of this by letting PHP handle the calendar logic internally.
Trade-offs and Pitfalls
Using strtotime() is concise and readable, but it has some quirks:
- It depends on the systemâs timezone settings. If your server is in UTC but you expect local time, adjust with
date_default_timezone_set(). 'this Monday'vs'next Monday'can be confusing depending on how you define âthis weekâ. ISO weeks start on Monday, so'this Monday'usually means the most recent Monday (including today if it is Monday). If you want strict consistency across environments, prefer explicit offsets likestrtotime('Monday of this week')or even calculate it manually usingDateTime.
For production code where clarity and portability matter, consider using the DateTime and DateInterval classes:
$monday = new DateTime('first day of this month'); // Not what we want yet
// Better approach:
$today = new DateTime();
$monday = clone $today;
$monday->modify('next Monday'); // or 'this Monday' depending on needs
echo $monday->format('d-m-Y');
This is more verbose but gives you full control and avoids strtotime()âs parsing ambiguities.
When to Use Laravelâs Helpers (Optional)
If you're working within a Laravel application, you can simplify further using the Carbon helper or Laravelâs built-in date functions. For example:
use Carbon\Carbon;
$monday = Carbon::now()->startOfWeek(); // Starts on Monday for ISO week
echo $monday->format('d-m-Y');
This is especially useful if you're already using Carbon in your project, as it provides a fluent interface and better timezone handling out of the box. However, for plain PHP scripts without Laravel, stick with strtotime() or DateTime for simplicity and performance.