PHP Dokumentation: Datetime createfromformat
12. Januar 2010 von werner
DateTime::createFromFormat
(PHP 5 >= 5.3.0)
DateTime::createFromFormat — Returns new DateTime object formatted according to the specified format
Beschreibung
Returns new DateTime object formatted according to the specified format.
Parameter-Liste
- format
Format accepted by date().
If format does not contain the character ! then portions of the date/time value specified in format but not specified in time will be set to the current system time.
If format contains the character !, then portions of the generated time specified to the left-hand side of the ! in format will be set to corresponding values from the Unix epoch.
If the first character of format is !, then all portions of the date/time value generated which are not specified in time will be initialized to corresponding values from the Unix epoch.
The Unix epoch is 1970-01-01 00:00:00 UTC.
- time
String representing the time.
- timezone
Time zone.
Rückgabewerte
Returns new DateTime instanceIm Fehlerfall wird FALSE zurückgegeben..
Beispiele
Beispiel #1 Using ! to reset default date/time values
<?php
echo "Current system date and time: " . date('Y-m-d H:i:s') . "\n";$format = 'Y-m-d';
$dt = DateTime::createFromFormat($format, '2009-02-03');
echo "Format: $format; " . $dt->date . "\n";$format = 'Y-m-d H:i:s';
$dt = DateTime::createFromFormat($format, '2009-02-03 15:16:17');
echo "Format: $format; " . $dt->date . "\n";$format = 'Y-m-!d H:i:s';
$dt = DateTime::createFromFormat($format, '2009-02-03 15:16:17');
echo "Format: $format; " . $dt->date . "\n";$format = '!Y-m-d';
$dt = DateTime::createFromFormat($format, '2009-02-03');
echo "Format: $format; " . $dt->date . "\n";
?>The above example will output something like the following (taking into account the current system time):
Current system date and time: 2009-09-13 01:04:03Format: Y-m-d; 2009-02-03 01:04:03Format: Y-m-d H:i:s; 2009-02-03 15:16:17Format: Y-m-!d H:i:s; 1970-01-03 15:16:17Format: !Y-m-d; 2009-02-03 00:00:00
Siehe auch
- DateTime::__construct – Returns new DateTime object
- DateTime::getLastErrors – Returns the warnings and errors