The Trap of strtotime and Date Parsing in PHP
You are encountering a classic PHP pitfall involving how the language handles date strings versus timestamps. Your goal is to convert an old date format into a new one, but the intermediate step using strtotime is failing silently, defaulting to the Unix epoch (1970-01-01 00:00:00).
The root cause lies in the output of your first line: $old_date = date('y-m-d-h-i-s');. This string contains literal lowercase letters y, m, d, h, i, and s. In PHP's date() function, these are format specifiers that generate values based on the current time. However, when you pass that resulting string to strtotime(), it does not recognize those letters as valid date components.
strtotime expects a human-readable representation of a date, such as "March 14, 2023" or "2023-03-14", along with relative terms like "next week". It cannot parse a string composed entirely of format codes. Consequently, strtotime fails to find a valid timestamp and returns false. When you pass false (or the integer 0) to the second date() call, it interprets this as January 1, 1970, which is why you see that specific date in your output.
To fix this, you must generate an actual ISO-formatted string before passing it to strtotime, or bypass strtotime entirely if you are simply trying to reformat a string.
Parsing with DateTime
If your input comes from an external source as a raw stringâsay, "2023-10-27 14:30:00"âand you want to normalize it or change the format, relying on strtotime is often unnecessary overhead. The modern and robust approach in PHP is to use the DateTime class.
You can create a DateTime object directly from your string using the constructor:
$inputDate = '2023-10-27 14:30:00'; // Example input string
$dt = new DateTime($inputDate);
// Convert to a different format immediately
$newFormat = 'd-m-Y H:i:s T';
$outputDate = $dt->format($newFormat);
echo $outputDate; // Output: 27-10-2023 14:30:00 GMT+0000 (depending on timezone)
This method is explicit and avoids the ambiguity of strtotime. It handles edge cases, such as timezones, more gracefully when combined with DateTimeZone. If you need to handle relative dates like "last Monday" or "next Tuesday", then DateTime::modify() works perfectly in conjunction with the constructor.
Handling Timezone Contexts
One critical detail often overlooked is timezone handling. The date() function outputs time in the server's configured timezone, while DateTime objects store absolute moments in time (UTC internally). If you convert a date string without accounting for timezones, the resulting hour might shift unexpectedly depending on your environment configuration.
If you are working with raw strings that lack timezone information, it is safer to let PHP assume the system default or explicitly set one:
$dt = new DateTime($inputDate);
$dt->setTimezone(new DateTimeZone('UTC')); // Or 'America/New_York'
$formatted = $dt->format('Y-m-d H:i:s');
When strtotime Actually Works
It is important to understand when strtotime succeeds so you don't discard a useful tool. It works well with natural language:
$nextWeek = strtotime('next Monday');
$dateString = date('Y-m-d', $nextWeek);
Here, strtotime parses the relative phrase "next Monday" and returns the correct Unix timestamp. However, as seen in your original code, it fails when given a string that looks like a date format definition rather than a date value. If you already have a string in Y-m-d H:i:s format (which is what date() generates), simply passing that string to another date() call with a new format specifier is sufficient; no conversion via strtotime is needed.
Best Practices for Date Manipulation
In larger applications, mixing strtotime and raw date() calls often leads to bugs related to timezone drift or malformed inputs. The DateTimeImmutable class is preferable in production code because it ensures immutability, preventing accidental state changes across your application logic.
$immutableDate = new DateTimeImmutable($inputString);
$formattedDate = $immutableDate->format('Y-m-d');
Using immutable objects simplifies testing and debugging, as you don't have to worry about one function call affecting another part of your codebase. While date() is concise for quick scripts, adopting the DateTime family provides a structured way to handle dates that scales better as complexity increases.
