In PHP, date_create() builds a new DateTime object from a date and time string. If the string cannot be understood, it returns false instead of throwing an error.
date_create() is the procedural version of new DateTime(). It accepts many text formats, such as '2024-01-15 10:30:00', '+1 week' or 'last day of February 2024'. The string 'now' (the default) means the current moment. The returned DateTime object can be changed after it is created.
Syntax
Call date_create() with the values you need. Start with this line.
PHP
$date = date_create('2024-01-15 10:30:00');
Call
What it does
date_create('2024-01-15')
Returns a DateTime for midnight on January 15, 2024.
date_create('2024-01-15 +1 week')
Understands relative text and returns January 22, 2024.
date_create('2024-01-15 10:30', new DateTimeZone('Asia/Tokyo'))
Reads the time as Tokyo time instead of the default timezone.
date_create() is the function form of new DateTime(), but with one important difference: new DateTime() throws an exception on a bad string, while date_create() returns false. Always check the result before you use it. For code that should not be changed by accident, use date_create_immutable().
Optional. A date and time string in a format PHP understands. The default is "now", the current time. An empty string also means now.
timezone
Optional. A DateTimeZone object. It is ignored if the string already contains a timezone or is a Unix timestamp written like '@1705314600'. If null, the default timezone is used.
Return Value
Return Value
Description
DateTime
A new DateTime object when the string could be parsed.
false
Returned when the string cannot be parsed.
PHP Examples
Example 1 — Creating Dates from Different Strings
date_create() understands absolute dates, relative text and natural phrases. Here three different strings give three DateTime objects.
The first string is a full date and time, and the result is a DateTime object.
date_format() prints it back using the Y-m-d H:i:s pattern.
'2024-01-15 +1 week' starts from the date and moves seven days forward.
'last day of February 2024' is a natural phrase, and 2024 is a leap year, so it gives the 29th.
Example 2 — Invalid Text Returns false
Unreadable text returns false. A date that only looks wrong, such as February 30, is accepted and rolled over.
PHP
<?php
var_dump(date_create('not a date'));
$d = date_create('2024-02-30');
echo date_format($d, 'Y-m-d'), PHP_EOL;
if (date_create('banana') === false) {
echo "Always check for false first", PHP_EOL;
}
Output
bool(false)
2024-03-01
Always check for false first
How It Works
'not a date' cannot be parsed, so date_create() returns false without any warning.
'2024-02-30' can be parsed, so PHP creates a DateTime and rolls it over to 2024-03-01.
Comparing the result with === false is the safe way to detect failure.
If you skip this check, calling a method on false causes a fatal error.
Example 3 — Using a Timezone Argument
Pass a DateTimeZone object as the second argument to read the time in a specific timezone, then convert it to UTC.
PHP
<?php
$tz = new DateTimeZone('Asia/Tokyo');
$date = date_create('2024-01-15 10:30:00', $tz);
echo date_format($date, 'Y-m-d H:i T P'), PHP_EOL;
echo date_timezone_get($date)->getName(), PHP_EOL;
date_timezone_set($date, new DateTimeZone('UTC'));
echo date_format($date, 'Y-m-d H:i T P'), PHP_EOL;
Output
2024-01-15 10:30 JST +09:00
Asia/Tokyo
2024-01-15 01:30 UTC +00:00
How It Works
The string has no timezone of its own, so the DateTimeZone argument is used.
T prints the abbreviation JST and P prints the offset +09:00.
date_timezone_get() returns the timezone object, and getName() gives its name.
date_timezone_set() converts the same moment to UTC, so 10:30 in Tokyo becomes 01:30 UTC.
Frequently Asked Questions
It creates a DateTime object from a date and time string. It is the procedural form of new DateTime() and returns false instead of throwing an exception when the string is invalid.
Both build a DateTime. new DateTime() throws an exception for an invalid string, while date_create() returns false. Choose the style that matches how you want to handle errors.
If the text cannot be parsed, it returns false. But dates that look wrong yet are parseable, like '2024-02-30', are rolled over to the next valid day and still return an object.
Call date_create() with no arguments, or pass 'now'. The result depends on when the code runs, so use a fixed date string in tests and examples.
Prefer date_create_immutable() when you want values that cannot be changed by mistake. date_create() returns a mutable DateTime, so methods like add() or modify() change the original object.