Anonymous user / 54.226.41.91 Log In Register
?
Wallet: 3.00
Daily Credits:
1.20 / 1.20

PHP Functions Online


DateTime::__construct

(PHP 5 >= 5.2.0)

DateTime::__construct -- date_createReturns new DateTime object

public DateTime::__construct([string $time = "now"[, DateTimeZone $timezone = NULL]])
public DateTimeZone::__construct(string $timezone)

Checkout ? #

Item Description Item Price Your Price
Total

Preview #

${{ variable.param.name }} = {{ variable.param|getValue:variable.form:true }};
{{ call.result}} = ;

Tip: You can change some values by clicking on it in the preview above.

Description #

public DateTime::__construct() ([ string $time = "now" [, DateTimeZone $timezone = NULL ]] )

Returns new DateTime object.

Parameters #

time

A date/time string. Valid formats are explained in Date and Time Formats.

Enter NULL 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).

Return Values #

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

Errors #

Emits Exception in case of an error.

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

Changelog #

Version Description
5.3.0 If time contains an invalid date/time format, then an exception is now thrown. Previously an error was emitted.

See Also #