downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

DateTime::createFromFormat> <DateTime::add
Last updated: Fri, 05 Feb 2010

view this page in

DateTime::__construct

(PHP 5 >= 5.2.0)

DateTime::__constructReturns new DateTime object

Description

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

Returns new DateTime object.

Parameters

time

String in a format accepted by strtotime().

timezone

Time zone of the time.

Errors/Exceptions

Emits Exception in case of an error.

Examples

Example #1 DateTime::__construct() example

<?php
date_default_timezone_set
('Europe/London');

$datetime = new DateTime('2008-08-03 14:52:10');
echo 
$datetime->format(DATE_ATOM);
?>



DateTime::createFromFormat> <DateTime::add
Last updated: Fri, 05 Feb 2010
 
add a note add a note User Contributed Notes
DateTime::__construct
paul at smartposition dot nl
03-Feb-2010 11:07
At least with php 5.2.12, the behavior of the DateTime constructor in combination with timezones surprised me a bit.
Apparently, the second argument (timezone) is completely ignored when the first argument (time) already contains timezone info;
I expected it to transform 'time' to 'timezone' instead. So, with my system being on 'Europe/Amsterdam' (+01:00):

<?php
$date
= new DateTime('2010-01-28T15:00:00' );
echo
$date->format( 'c' ); // Outputs: '2010-01-28T15:00:00+01:00'
?>

Works as expected, and so does:

<?php
$date
= new DateTime('2010-01-28T15:00:00', new DateTimezone( 'America/Chicago' ));
echo
$date->format( 'c' ); // Outputs: '2010-01-28T15:00:00-06:00'
?>

However:

<?php
$date
= new DateTime('2010-01-28T15:00:00+02:00', new DateTimezone( 'UTC' ) );
echo
$date->format( 'c' ); // Outputs '2010-01-28T15:00:00+02:00'
?>

Which is the same output as when the second argument isn't present at all; I'd expect the date to be transformed to the given DateTimezone.
To achieve this effect however, you'll need an additional 'setTimezone' call:

<?php
$date
= new DateTime('2010-01-28T15:00:00+02:00' );
echo
$date->format( 'c' ); // Outputs '2010-01-28T15:00:00+02:00'

$date->setTimezone( new DateTimezone( 'UTC' ) );
echo
$date->format( 'c' ); // Outputs '2010-01-28T13:00:00+00:00'
?>
kendsnyder at gmail dot com
12-Jan-2010 04:37
Also forgot to mention, that MySQL "zeroed" dates do not throw an error but produce a non-sensical date:

<?php

$d
= new DateTime("0000-00-00");
$d->format("Y-m-d"); // "-0001-11-30"

?>

Another good reason to write your own class that extends from DateTime.
kendsnyder at gmail dot com
12-Jan-2010 04:24
The theoretical limits of the date range seem to be "-9999-01-01" through "9999-12-31" (PHP 5.2.9 on Windows Vista 64):

<?php

$d
= new DateTime("9999-12-31");
$d->format("Y-m-d"); // "9999-12-31"

$d = new DateTime("0000-12-31");
$d->format("Y-m-d"); // "0000-12-31"

$d = new DateTime("-9999-12-31");
$d->format("Y-m-d"); // "-9999-12-31"

?>

Dates above 10000 and below -10000 do not throw errors but produce weird results:

<?php

$d
= new DateTime("10019-01-01");
$d->format("Y-m-d"); // "2009-01-01"

$d = new DateTime("10009-01-01");
$d->format("Y-m-d"); // "2009-01-01"

$d = new DateTime("-10019-01-01");
$d->format("Y-m-d"); // "2009-01-01"

?>
thomas at koch dot ro
17-Nov-2009 04:26
Derick confirmed, that the following is expected behaviour:

<?php
var_dump
( date_default_timezone_get() );
$dateTime = new DateTime( '23:01' );
var_dump( $dateTime->getTimezone()->getName() );

$dateTime = new DateTime( '@1234567890' );
var_dump( $dateTime->getTimezone()->getName() );
?>

Output:
string(13) "Europe/Berlin"
string(13) "Europe/Berlin"
string(6) "+00:00"

So depending on the format you use to define the DateTime object it either receives the default timezone or not.
alvaro at demogracia dot com
04-Aug-2009 12:09
Like in many other date functions, PHP calculates the correct value for out-of-range input, so the constructor won't necessarily throw an exception when fed with wrong dates:

<?php

date_default_timezone_set
('Europe/Madrid');

try{
   
$d1 = new DateTime('2009-02-28');
    echo
$d1->format(DATE_RSS) . "\n";
}catch(
Exception $e){
    echo
"Invalid date: 2009-02-28\n";
}

try{
   
$d2 = new DateTime('2009-02-29');
    echo
$d2->format(DATE_RSS) . "\n";
}catch(
Exception $e){
    echo
"Invalid date: 2009-02-29\n";
}

try{
   
$d3 = new DateTime('2009-02-xx');
    echo
$d3->format(DATE_RSS) . "\n";
}catch(
Exception $e){
    echo
"Invalid date: 2009-02-xx\n";
}

?>

Sat, 28 Feb 2009 00:00:00 +0100
Sun, 01 Mar 2009 00:00:00 +0100
Invalid date: 2009-02-xx

DateTime::createFromFormat> <DateTime::add
Last updated: Fri, 05 Feb 2010
 
 
show source | credits | sitemap | contact | advertising | mirror sites