Menu Home

date_create

osbo.com | PHP | Functions | date_create

Returns new DateTime object

Syntax

date_create ([ string $datetime = "now" [, DateTimeZone $timezone = NULL ]] ) : DateTime

Parameters

datetime

A date/time string. Valid formats are explained in Date and Time Formats. Enter "now" here to obtain the current time when using the $timezone parameter.

timezone

A DateTimeZone object representing the timezone of $datetime. If $timezone is omitted, the current timezone will be used. Note: The $timezone parameter and the current timezone are ignored when the $datetime parameter either is a UNIX timestamp (e.g. @946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00).

Return

Returns a new DateTime instance. Procedural style returns FALSE on failure.

Examples

void

<?

$return = date_create();

echo date_format($return, "Y-m-d H:i:s");

?>
2021-04-23 09:11:49

datetime

<?

$datetime = "2001-01-01";

$return = date_create($datetime);

echo date_format($return, "Y-m-d H:i:s");

?>
2001-01-01 00:00:00

timezone

<?

$datetime = "now";
$timezone = timezone_open("America/New_York");

$return = date_create($datetime, $timezone);

echo date_format($return, "Y-m-d H:i:s");

?>
2021-04-23 05:11:50

PHP