Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

date_create()

Example

Return a new DateTime object and then format the date.

<?php
$date=date_create(“2013-03-15”);
echo date_format($date,“Y/m/d”);
?>

Definition and Usage

The date_create() function returns a new instance of the DateTime object.

Syntax

date_create(time, timezone)

Parameter Values

Parameter

Description

time

Optional. Specifies a date/time string; NULL indicates the current time.

timezone

Optional. Specifies the timezone for the time; defaults to the current timezone.

Technical Details

Return Value:

Returns a new DateTime object on success; FALSE or an Exception on failure.

PHP Version:

5.2+

PHP Changelog:

In PHP 7.1, microseconds are now populated with their actual value rather than just “00000”. In PHP 5.3, an exception is thrown for invalid time values, whereas previously an error was generated.

More Examples

Example

Return a new DateTime object (with the specified timezone) and then format 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”);
?>