What are PHP date and time functions?
Why Interviewers Ask This
Interviewers use this question to quickly assess whether a candidate has the foundational knowledge required for PHP development. It reveals whether you understand the building blocks that more complex concepts rely on.
Answer
PHP has comprehensive date/time functions. time() returns the current Unix timestamp (seconds since January 1, 1970 UTC). date($format, $timestamp) formats a timestamp — e.g., date("Y-m-d H:i:s") gives "2024-01-15 14:30:00". strtotime($str) converts a human-readable date string to a Unix timestamp. The modern, object-oriented approach uses the DateTime and DateTimeImmutable classes along with DateInterval and DateTimeZone — these handle timezone conversions, date arithmetic, and formatting more cleanly than procedural functions. Always store dates in UTC in the database and convert to user timezones only for display.
Common Mistake
Many candidates answer correctly but can't explain the 'why'. Always be prepared to justify your answer with a concrete example or use case from your PHP experience.
Previous
What is the difference between intval(), floatval(), and strval()?
Next
How do you connect to a MySQL database in PHP?