PHP
date function
MySQL
datetime format
database insertion

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.

Practice system design

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:

text
2025-09-23 14:28:35

In PHP, the matching date() format string is:

php
Y-m-d H:i:s

For example:

php
1<?php
2
3date_default_timezone_set('UTC');
4
5$value = date('Y-m-d H:i:s');
6echo $value;

That gives you a string compatible with a MySQL DATETIME column.

Basic Insert Example

A modern safe insert should use a prepared statement:

php
1<?php
2
3$pdo = new PDO('mysql:host=localhost;dbname=appdb;charset=utf8mb4', 'user', 'pass');
4date_default_timezone_set('UTC');
5
6$createdAt = date('Y-m-d H:i:s');
7
8$stmt = $pdo->prepare('INSERT INTO events (name, created_at) VALUES (?, ?)');
9$stmt->execute(['Sample Event', $createdAt]);

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.

php
1<?php
2
3$dt = new DateTimeImmutable('now', new DateTimeZone('UTC'));
4$mysqlValue = $dt->format('Y-m-d H:i:s');
5
6echo $mysqlValue;

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:

  • 'DATETIME stores a literal date-time value without timezone conversion'
  • 'TIMESTAMP has 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.

php
1<?php
2
3$raw = '09/23/2025 02:28 PM';
4$dt = DateTimeImmutable::createFromFormat('m/d/Y h:i A', $raw, new DateTimeZone('UTC'));
5
6if ($dt === false) {
7    throw new RuntimeException('Invalid date input');
8}
9
10$mysqlValue = $dt->format('Y-m-d H:i:s');
11echo $mysqlValue;

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:

sql
INSERT INTO events (name, created_at)
VALUES ('Sample Event', NOW());

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 DATETIME is Y-m-d H:i:s.
  • Use prepared statements when sending the value to MySQL.
  • Prefer explicit timezone handling, ideally with UTC.
  • 'DateTimeImmutable is often cleaner and safer than raw date() for non-trivial cases.'
  • If the database time should be authoritative, consider using MySQL NOW() instead.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.