date_create_immutableRETURNS a new
DateTimeImmutable object.
<?php
/* - - - - - - - - - - - - - - - - -
Object oriented style
- - - - - - - - - - - - - - - - - */
public DateTimeImmutable::__construct ( str $datetime = "now" ,
DateTimeZone|null $timezone = null )
where,
$datetime = A date/time STRING according the Date and Time Formats
$timezone = A DateTimeImmutable object representing the TIMEZONE
?>
<?php
/* - - - - - - - - - - - - - - - - -
Procedural style
- - - - - - - - - - - - - - - - - */
DateTimeImmutable|false date_create_immutable ( str $datetime = "now" ,
DateTimeZone|null $timezone = null )
where,
$datetime = A date/time STRING according the Date and Time Formats
$timezone = A DateTimeImmutable object representing the TIMEZONE
?>
$datetime
STRING representing the date/time according the Date and Time Formats.
$timezone
A DateTimeImmutable object representing the TimeZone.
This function works like DateTime::__construct but works with DateTimeImmutable.
EXERCISE
<?php
$obj01 = date_create_immutable('Invalid');
var_dump($obj01);
?>
EXERCISE
<?php
$tz = new DateTimeZone("Asia/Tokyo");
$current = "now";
echo "getTimezone():<br><br>";
$v = date_create_immutable($current);
$x = $v->getTimezone();
var_dump($x->getName());
echo "<br><br>getTimestamp():<br><br>";
$v = date_create_immutable($current);
$x = $v->getTimestamp();
var_dump($x);
?>
EXERCISE
<?php
$nw_tz03 = new DateTimeZone('America/New_York');
$nw_dt03 = '2005-03-08 08:33:33';
$obj03 = (array) date_create_immutable($nw_dt03, $nw_tz03);
// convert object to array
echo 'print_r($obj03):<br>';
print_r($obj03);
echo '<br><br>';
foreach($obj03 as $k03 => $v03)
echo "[$k03] => $v03<br>";
?>
EXERCISE
<?php
$nw_tz04 = new DateTimeZONE('Africa/Nairobi');
$nw_dt04 = 'now';
$obj04 = (array) date_create_immutable($nw_dt04, $nw_tz04);
// convert object to array
echo 'print_r($obj04):<br>';
print_r($obj04);
echo '<br><br>';
foreach($obj04 as $k04 => $v04)
echo "[$k04] => $v04<br>";
?>
EXERCISE
<?php
$dtm051 = new DateTimeImmutable('UTC');
$dtz051 = new DateTimeZone('UTC');
$str051 = '2013-05-08 08:33:33';
echo 'public __construct:<br>';
$dtm051->__construct($str051, $dtz051);
echo '$dtm051->__construct($str051, $dtz051)';
echo '<br><br>public string format:<br>';
$str051_frmt = $dtm051->format(DATE_W3C);
echo $str051_frmt;
echo '<br><br>NOW<br><br>public __construct:<br>';
$dtm051->__construct("now", $dtz051);
echo '$dtm051->__construct("now", $dtz051)';
echo '<br><br>public string format:<br>';
$str051_frmt = $dtm051->format(DATE_W3C);
echo $str051_frmt;
?>
EXERCISE
<?php
$dtm061 = new DateTimeImmutable('UTC');
$dtz061 = new DateTimeZone('UTC');
$str061 = '2013-05-08 08:33:33';
echo 'public __construct:<br>';
$dtm061->__construct($str061, $dtz061);
echo '$dtm061->__construct($str061, $dtz061)';
echo '<br><br>public string format:<br>';
$str061_frmt = $dtm061->format(DATE_W3C);
echo $str061_frmt;
$dtz062 = new DateTimeZone('America/Sao_Paulo');
echo '<br><br><br><br>public __construct:<br>';
$dtm061->__construct("now", $dtz062);
echo '$dtm061->__construct("now", $dtz062)';
echo '<br><br>public string format:<br>';
$str061_frmt = $dtm061->format(DATE_W3C);
echo $str061_frmt;
?>
EXERCISE
<?php
$dtm071 = new DateTimeImmutable('America/Sao_Paulo');
$dtz071 = new DateTimeZone('America/Sao_Paulo');
$str071 = '2012-06-05 21:00:00';
$dtm071->__construct($str071, $dtz071);
echo '<br>IPV6 PROTOCOL<br>INITIAL DATE OF OPERATION IN BRAZIL:<br>';
$str071_frmt = $dtm071->format(DATE_W3C);
echo $str071_frmt;
?>
EXERCISE
<?php
date_default_timezone_set('UTC');
$db1 = new DateTimeImmutable( '2008-01-01' );
$db2 = new DateTime( '2008-01-01' );
$de = new DateTime( '2008-03-31' );
$di = DateInterval::createFromDateString( 'first day of next month' );
foreach ( new DatePeriod( $db1, $di, $de ) as $dt )
{
echo '<u>'. get_class( $dt ) . "</u><br><br>";
echo $dt->format( "l Y-m-d" );
echo '<br>';
echo $dt->modify( "3 tuesday" )->format( "l Y-m-d" );
echo '<br>';
echo $dt->format( "l Y-m-d" );
echo '<br><br>';
}
foreach ( new DatePeriod( $db2, $di, $de ) as $dt )
{
echo '<u>'. get_class( $dt ) . "</u><br><br>";
echo $dt->format( "l Y-m-d" );
echo '<br>';
echo $dt->modify( "3 tuesday" )->format( "l Y-m-d" );
echo '<br>';
echo $dt->format( "l Y-m-d" );
echo '<br><br>';
}
?>
EXERCISE
<?php
$tz = new DateTimeZone("Asia/Tokyo");
$current = "2012-12-27 16:24:08";
function dump($a, $b, $c)
{ echo $a->format('e'), "<br>";
echo 'orig: ', $a->format('Y-m-d H:i:s'), "<br><br>";
echo $b->format('e'), "<br>";
echo 'copy: ', $b->format('Y-m-d H:i:s'), "<br><br>";
echo $c->format('e'), "<br>";
echo 'changed: ', $c->format('Y-m-d H:i:s'), "<br><br>";
}
echo "modify():<br><br>";
$v = date_create_immutable($current);
$z = $v;
$x = $z->modify("+2 days");
dump($v, $z, $x);
$v = date_create($current);
$z = $v;
$x = $z->modify("+2 days");
dump($v, $z, $x);
echo "<br><br>add():<br><br>";
$v = date_create_immutable($current);
$z = $v;
$x = $z->add(new DateInterval("P2DT2S"));
dump($v, $z, $x);
$v = date_create($current);
$z = $v;
$x = $z->add(new DateInterval("P2DT2S"));
dump($v, $z, $x);
echo "<br><br>sub():<br><br>";
$v = date_create_immutable($current);
$z = $v;
$x = $z->sub(new DateInterval("P2DT2S"));
dump($v, $z, $x);
$v = date_create($current);
$z = $v;
$x = $z->sub(new DateInterval("P2DT2S"));
dump($v, $z, $x);
echo "<br><br>setTimezone():<br><br>";
$v = date_create_immutable($current);
$z = $v;
$x = $z->setTimezone($tz);
dump($v, $z, $x);
$v = date_create($current);
$z = $v;
$x = $z->setTimezone($tz);
dump($v, $z, $x);
$v = new DateTimeImmutable($current);
$z = $v;
$x = $z->setTimezone($tz);
dump($v, $z, $x);
echo "<br><br>setTime():<br><br>";
$v = date_create_immutable($current);
$z = $v;
$x = $z->setTime(5, 7, 19);
dump($v, $z, $x);
$v = date_create($current);
$z = $v;
$x = $z->setTime(5, 7, 19);
dump($v, $z, $x);
echo "<br><br>setDate():<br><br>";
$v = date_create_immutable($current);
$z = $v;
$x = $z->setDate(5, 7, 19);
dump($v, $z, $x);
$v = date_create($current);
$z = $v;
$x = $z->setDate(5, 7, 19);
dump($v, $z, $x);
echo "<br><br>setIsoDate():<br><br>";
$v = date_create_immutable($current);
$z = $v;
$x = $z->setIsoDate(2012, 2, 6);
dump($v, $z, $x);
$v = date_create($current);
$z = $v;
$x = $z->setIsoDate(2012, 2, 6);
dump($v, $z, $x);
echo "<br><br>setTimestamp():<br><br>";
$v = date_create_immutable($current);
$z = $v;
$x = $z->setTimestamp(2012234222);
dump($v, $z, $x);
$v = date_create($current);
$z = $v;
$x = $z->setTimestamp(2012234222);
dump($v, $z, $x);
?>