W3docs

date_create()

In this article, we'll explore how to create dates using PHP's date_create() function. We'll cover the syntax of the function and its parameters, and we'll

Introduction

PHP's date_create() function is the procedural way to build a DateTime object — the object-oriented building block for almost all date and time work in PHP. This article covers its syntax and parameters, shows how to read a DateTime once you have one, explains what happens when parsing fails, and points to the related functions you'll reach for next.

date_create() is simply an alias for new DateTime(). The two are interchangeable, so use whichever reads better in your code. A common reason to prefer date_create() is that, unlike the constructor, it returns false instead of throwing an exception on a bad input string — which can be handled without a try/catch block.

The date_create() Function

The date_create() function is a built-in PHP function that creates a new DateTime object. It takes an optional parameter that specifies the date and time in a format recognized by the strtotime() function. If no parameter is passed, the function returns a DateTime object representing the current date and time.

Syntax

The syntax for the date_create() function is as follows:

The date_create() Function syntax in PHP

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

The first parameter, $time, specifies the date and time to create the DateTime object. It is optional and has a default value of "now". The second parameter, $timezone, specifies the timezone to use. It is also optional and has a default value of NULL.

Parameters

Let's take a closer look at the parameters of the date_create() function:

  • $datetime (optional): Specifies the date and time to create the DateTime object. It can be any string in a format recognized by the strtotime() function — an absolute date like "2022-12-31", a relative phrase like "next monday" or "+1 week", or a UNIX timestamp prefixed with @ (for example "@1672531199"). The default "now" produces the current date and time.
  • $timezone (optional): A DateTimeZone object specifying the timezone. If omitted (or null), the script's default timezone is used. Note: this parameter is ignored when $datetime is a UNIX timestamp (@...) or already contains a timezone offset such as "2022-12-31 23:59:59+02:00".

Return value

On success, date_create() returns a DateTime object. On failure — for example when $datetime cannot be parsed — it returns false rather than throwing an exception. This makes the result safe to test with a simple condition:

<?php

$date = date_create('not a real date');

if ($date === false) {
    echo 'Could not parse the date.';
} else {
    echo $date->format('Y-m-d');
}
// Output: Could not parse the date.

Examples

Here are some examples of how to use the date_create() function:

Example of using date_create() function in PHP

php— editable, runs on the server

Using relative date strings

Because date_create() accepts anything strtotime() understands, you can build dates relative to "now" without any arithmetic:

<?php

$today = date_create();
echo $today->format('l') . "\n";          // e.g. Output: Thursday

$nextWeek = date_create('+1 week');
echo $nextWeek->format('Y-m-d') . "\n";   // 7 days from today

$firstOfMonth = date_create('first day of this month');
echo $firstOfMonth->format('Y-m-d');       // e.g. 2023-03-01

Working with the result

Once you have a DateTime object, you typically format it for display or compare it with another date:

<?php

$start = date_create('2022-01-01');
$end   = date_create('2022-12-31');

// Format the object as a string
echo $end->format('F j, Y') . "\n";   // Output: December 31, 2022

// Compare two DateTime objects directly
echo ($end > $start ? 'end is later' : 'start is later');
// Output: end is later

Use date_format() (or the ->format() method shown above) to render the object, and date_diff() to measure the interval between two DateTime objects.

  • date_create_from_format() — create a DateTime from a string that uses a custom format you specify, instead of relying on strtotime() heuristics.
  • date_format() — convert a DateTime object into a formatted string.
  • date_add() — add a DateInterval to a DateTime.
  • strtotime() — the parser that powers the $datetime argument; returns a UNIX timestamp instead of an object.

Conclusion

The date_create() function is the procedural entry point for working with PHP's DateTime objects. It accepts both absolute and relative date strings, optionally takes a timezone, and returns false on failure so you can validate input without exception handling. Once you have a DateTime, format it with ->format() and compare or subtract objects directly. For parsing non-standard input, reach for date_create_from_format(); to render results, use date_format().

Practice

Practice
What does the PHP date_create() function do?
What does the PHP date_create() function do?
Was this page helpful?