PHP date_create() Function

Example

Returns a new DateTime object and then formats the date:

<?php
$date=date_create("2016-09-25");
echo date_format($date,"Y/m/d");
?>

Run Instances

Definition and Usage

The date_create() function returns a new DateTime object.

Syntax

date_create(time,timezone);
Parameters Description
time Optional. Specify the date/time string. NULL indicates the current date/time.
timezone

Optional. Specify the timezone for the time. The default is the current timezone.

Hint:View the list of all timezones supported in PHP.

Technical Details

Return Value: Returns a new DateTime object if successful, or FALSE if unsuccessful.
PHP Version: 5.2+
Update Log: Starting from PHP 5.3+, if an invalid date is specified, an exception is thrown.

More Examples

Example 1

Returns a new DateTime object with the specified timezone and then formats the date and time:

<?php
$date=date_create("2013-03-15 23:40:00",timezone_open("Europe/Oslo"));
echo date_format($date,"Y/m/d H:iP");
?>

Run Instances