PHP date format when inserting into datetime in MySQL
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
When inserting a value into a MySQL DATETIME column from PHP, the standard format is Y-m-d H:i:s. The more important detail, though, is not only the string format. It is making sure you generate the timestamp in the correct timezone and pass it into the query safely.
The MySQL DATETIME Format
A MySQL DATETIME value looks like this:
In PHP, the matching date() format string is:
For example:
That gives you a string compatible with a MySQL DATETIME column.
Basic Insert Example
A modern safe insert should use a prepared statement:
This does two things correctly:
- uses the right datetime string format
- avoids unsafe string interpolation in SQL
The second point matters just as much as the first in real applications.
Prefer DateTime Over Raw date()
PHP’s date() function is fine for simple cases, but DateTimeImmutable usually leads to cleaner code when timezones matter.
This makes the timezone explicit and avoids relying too heavily on global runtime state.
That becomes useful when your application:
- stores timestamps in UTC
- displays them in local time later
- works across multiple servers or regions
DATETIME vs TIMESTAMP
This question is often really about DATETIME, but you should know the storage distinction:
- '
DATETIMEstores a literal date-time value without timezone conversion' - '
TIMESTAMPhas behavior tied more closely to timezone conversion rules in MySQL'
If you use DATETIME, the value inserted is the value stored. That makes consistent timezone handling in PHP even more important.
A common strategy is:
- generate the datetime in UTC in PHP
- store it in MySQL as UTC
- convert it only when presenting to users
That keeps the database values predictable.
Inserting Existing Date Strings
If the incoming date string is not already in MySQL format, convert it first instead of inserting it directly.
This is much safer than hoping MySQL will interpret an arbitrary date string the way you intend.
Let MySQL Generate It When Appropriate
If you simply need the current database time, MySQL can generate it directly:
That is sometimes cleaner than formatting a PHP date string at all.
Use PHP-generated dates when:
- the application decides the time value
- you need a specific timezone normalization path
- the datetime comes from user input or another system
Use NOW() when the database server’s current time is the intended source of truth.
Common Pitfalls
The most common pitfall is using the wrong format string, such as a human-readable format that MySQL may not parse consistently.
Another mistake is building SQL by concatenating the datetime string directly into the query instead of using a prepared statement.
A third issue is ignoring timezones. The string may match MySQL’s expected format perfectly and still represent the wrong real-world moment.
Finally, developers sometimes insert loosely formatted user input directly without parsing and normalizing it first. That can silently produce bad data.
Summary
- The standard PHP format for inserting into MySQL
DATETIMEisY-m-d H:i:s. - Use prepared statements when sending the value to MySQL.
- Prefer explicit timezone handling, ideally with UTC.
- '
DateTimeImmutableis often cleaner and safer than rawdate()for non-trivial cases.' - If the database time should be authoritative, consider using MySQL
NOW()instead.
Related reading
- PHP MySQL Google Chart JSON - Complete Example
- PHP MySQL transactions examples
- php mysqli_connect authentication method unknown to the client caching_sha2_password
- PHP PDO charset, set names?
- PHP with MySQL 8.0 error The server requested authentication method unknown to the client
- phpMyAdmin - Error Incorrect format parameter?
- phpmyadmin logs out after 1440 secs
- phpmyadmin.pma_table_uiprefs doesn't exist

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.