DateTime::__construct
date_create
(PHP 5 >= 5.2.0, PHP 7)
DateTime::__construct -- date_create — Returns new DateTime object
Description
Object oriented style
Procedural style
Returns new DateTime object.
Parameters
-
time
-
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
$time
.If
$timezone
is omitted, the current timezone will be used.Note:
The
$timezone
parameter and the current timezone are ignored when the$time
parameter either is a UNIX timestamp (e.g. @946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00).
Changelog
Version | Description |
---|---|
7.1 | From now on microseconds are filled with actual value. Not with '00000'. |
5.3.0 |
If time contains an invalid
date/time format, then an
exception is now thrown. Previously an error was emitted.
|
Examples
Example #1 DateTime::__construct() example
Object oriented style
<?php
try {
$date = new DateTime('2000-01-01');
} catch (Exception $e) {
echo $e->getMessage();
exit(1);
}
echo $date->format('Y-m-d');
?>
Procedural style
<?php
$date = date_create('2000-01-01');
if (!$date) {
$e = date_get_last_errors();
foreach ($e['errors'] as $error) {
echo "$error\n";
}
exit(1);
}
echo date_format($date, 'Y-m-d');
?>
The above examples will output:
2000-01-01
Example #2 Intricacies of DateTime::__construct()
<?php
// Specified date/time in your computer's time zone.
$date = new DateTime('2000-01-01');
echo $date->format('Y-m-d H:i:sP') . "\n";
// Specified date/time in the specified time zone.
$date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "\n";
// Current date/time in your computer's time zone.
$date = new DateTime();
echo $date->format('Y-m-d H:i:sP') . "\n";
// Current date/time in the specified time zone.
$date = new DateTime(null, new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "\n";
// Using a UNIX timestamp. Notice the result is in the UTC time zone.
$date = new DateTime('@946684800');
echo $date->format('Y-m-d H:i:sP') . "\n";
// Non-existent values roll over.
$date = new DateTime('2000-02-30');
echo $date->format('Y-m-d H:i:sP') . "\n";
?>
The above example will output something similar to:
2000-01-01 00:00:00-05:00 2000-01-01 00:00:00+12:00 2010-04-24 10:24:16-04:00 2010-04-25 02:24:16+12:00 2000-01-01 00:00:00+00:00 2000-03-01 00:00:00-05:00
See Also
- DateTime::createFromFormat() - Parses a time string according to a specified format
- DateTimeZone::__construct() - Creates new DateTimeZone object
- Date and Time Formats
- date.timezone ini setting
- date_default_timezone_set() - Sets the default timezone used by all date/time functions in a script
- DateTime::getLastErrors() - Returns the warnings and errors
- checkdate() - Validate a Gregorian date
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-datetime.construct.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.