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, 30 Oct 2009

view this page in

DateTime::__construct

(PHP 5 >= 5.2.0)

DateTime::__constructReturns new DateTime object

Beschreibung

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

Returns new DateTime object.

Parameter-Liste

time

String in a format accepted by strtotime(), defaults to "now".

timezone

Time zone of the time.

Fehler/Exceptions

Emits Exception in case of an error.

Beispiele

Beispiel #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);
?>



add a note add a note User Contributed Notes
DateTime::__construct
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, 30 Oct 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites