PHP date_create() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Date/Time

Definition

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');
CallWhat 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().

Library declarations
PHP
date_create(string $datetime = "now", ?DateTimeZone $timezone = null): DateTime|false

Parameters

ParameterDescription
datetimeOptional. A date and time string in a format PHP understands. The default is "now", the current time. An empty string also means now.
timezoneOptional. 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 ValueDescription
DateTimeA new DateTime object when the string could be parsed.
falseReturned 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.

PHP
<?php
$date = date_create('2024-01-15 10:30:00');
echo get_class($date), PHP_EOL;
echo date_format($date, 'Y-m-d H:i:s'), PHP_EOL;

$next = date_create('2024-01-15 +1 week');
echo date_format($next, 'Y-m-d'), PHP_EOL;

$last = date_create('last day of February 2024');
echo date_format($last, 'Y-m-d'), PHP_EOL;

How It Works

  1. The first string is a full date and time, and the result is a DateTime object.
  2. date_format() prints it back using the Y-m-d H:i:s pattern.
  3. '2024-01-15 +1 week' starts from the date and moves seven days forward.
  4. '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;
}

How It Works

  1. 'not a date' cannot be parsed, so date_create() returns false without any warning.
  2. '2024-02-30' can be parsed, so PHP creates a DateTime and rolls it over to 2024-03-01.
  3. Comparing the result with === false is the safe way to detect failure.
  4. 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;

How It Works

  1. The string has no timezone of its own, so the DateTimeZone argument is used.
  2. T prints the abbreviation JST and P prints the offset +09:00.
  3. date_timezone_get() returns the timezone object, and getName() gives its name.
  4. 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.

Next: date_create_from_format()

date_create_from_format() builds a DateTime object from a date string that follows a forma.

date_create_from_format() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful