Mastering PHP’s strtotime(): Convert Human Dates to UNIX Timestamps
Learn how PHP’s powerful strtotime() function transforms human‑readable date strings into UNIX timestamps, covering its syntax, common formats—including relative dates, absolute dates, time intervals, and special keywords—plus usage tips, limitations, and advanced parameters for flexible date handling.
strtotime()is a powerful and flexible PHP function that converts human‑readable date and time strings into UNIX timestamps.
A UNIX timestamp is an integer representing the number of seconds elapsed since 00:00:00 UTC on 1 January 1970.
Basic Syntax of strtotime()
strtotime(string $time, int $now = time()): int|falseIn this signature, $time is the date/time string to be converted, and $now (optional) provides a base timestamp for relative calculations. The function returns a UNIX timestamp or false on failure.
Common Format Examples
Relative dates and times such as "now", "tomorrow", "next week".
strtotime("tomorrow") // returns the UNIX timestamp for tomorrowAbsolute dates and times.
strtotime("2023-10-31") // timestamp for the given date
strtotime("2023-10-31 08:30:00") // timestamp for the given date and timeRelative time intervals.
strtotime("+1 day") // timestamp for one day later
strtotime("+1 week 2 days 4 hours 30 minutes") // timestamp for the specified intervalSpecial keywords such as "last day of month" or "first day of next month".
strtotime("last day of month") // timestamp for the last day of the current month
strtotime("first day of next month") // timestamp for the first day of the next monthNote that strtotime() has limited parsing capabilities; it may not handle uncommon or malformed date formats. Ensure input strings follow supported patterns.
Additionally, strtotime() supports extra parameters like time zone settings or specifying weekdays, which can further enhance its flexibility.
Summary
strtotime()is a highly useful PHP function for converting readable date strings into UNIX timestamps, enabling easy date arithmetic. However, its parsing is limited, so verify that input formats are standard before using it.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
