date_createRETURNS new
DateTime object.
<?php
/* - - - - - - - - - - - - - - - - -
Object oriented style
- - - - - - - - - - - - - - - - - */
public DateTime::__construct ( str $datetime = "now" ,
DateTimeZone|null $timezone = null )
where,
$datetime = A date/time STRING according the Date and Time formats
$timezone = A DateTimeZone object representing the TimeZone
?>
<?php
/* - - - - - - - - - - - - - - - - -
Procedural style
- - - - - - - - - - - - - - - - - */
DateTime|false date_create ( str $datetime = "now",
DateTimeZone|null $timezone = null )
where,
$datetime = A date/time STRING according the Date and Time formats
$timezone = A DateTimeZone object representing the TimeZone
?>
$datetime
A date/time STRING according the Date and Time formats.
$timezone
A DateTimeZone object representing the TimeZone.
Enter "now" for the $datetime to obtain the current time when using the $timezone.
$timezone is a DateTimeZone object representing the TimeZone of $datetime.
If $timezone is omitted, the current timezone will be used.
The $timezone and the current timezone are ignored when the $datetime parameter either is a UNIX timestamp or specifies a TimeZone.
EXERCISE
<?php
/*
$arrdc01 = date_create("now");
echo '<pre>';
var_dump($arrdc01);
echo '</pre>';
*/
$arrdc01 = (array)date_create("now");
// converting the object to array
foreach($arrdc01 as $kdc01 => $vdc01)
echo $kdc01 . ' => ' . $vdc01 . '<br><br>';
?>
EXERCISE
<?php
$arrdc02 = new DateTime("now");
echo '<pre>';
print_r($arrdc02);
echo '</pre>';
// try also
/*
$arrdc02 = (array)new DateTime("now");
// converting the object to array
foreach($arrdc02 as $kdc02 => $vdc02)
echo $kdc02 . ' => ' . $vdc02 . '<br><br>';
*/
?>
EXERCISE
<?php
date_default_timezone_set('Asia/Tokyo');
$obj03 = date_create();
echo 'print_r($obj03):<pre>';
print_r($obj03);
echo '</pre>';
?>
EXERCISE
<?php
date_default_timezone_set('America/Sao_Paulo');
$obj04 = date_create('1945-03-08 08:33:33');
echo 'print_r($obj04):<pre>';
print_r($obj04);
echo '</pre>';
?>
EXERCISE
<?php
$nw_tz05 = new DateTimeZone('America/New_York');
$nw_dt05 = '2005-03-08 08:33:33';
$obj05 = date_create($nw_dt05, $nw_tz05);
echo 'print_r($obj05):<pre>';
print_r($obj05);
echo '</pre>';
?>
EXERCISE
<?php
$nw_tz06 = new DateTimeZone('Africa/Nairobi');
$nw_dt06 = '2014/04/08 08:33:33';
$obj06 = date_create($nw_dt06, $nw_tz06);
echo 'print_r($obj06):<pre>';
print_r($obj06);
echo '</pre>';
?>
EXERCISE
<?php
$dtm071 = new DateTimeImmutable('UTC');
$dtz071 = new DateTimeZone('UTC');
$str071 = '2013-05-08 08:33:33';
echo 'public __construct:<br>';
$dtm071->__construct($str071, $dtz071);
echo '$dtm071->__construct($str071, $dtz071)';
echo '<br><br>public string format:<br>';
$str071_frmt = $dtm071->format(DATE_W3C);
echo $str071_frmt;
echo '<br><br>NOW<br><br>public __construct:<br>';
$dtm071->__construct("now", $dtz071);
echo '$dtm071->__construct("now", $dtz071)';
echo '<br><br>public string format:<br>';
$str071_frmt = $dtm071->format(DATE_W3C);
echo $str071_frmt;
?>
EXERCISE
<?php
$dtm081 = new DateTimeImmutable('UTC');
$dtz081 = new DateTimeZone('UTC');
$str081 = '2013-05-08 08:33:33';
echo 'public __construct:<br>';
$dtm081->__construct($str081, $dtz081);
echo '$dtm081->__construct($str081, $dtz081)';
echo '<br><br>public string format:<br>';
$str081_frmt = $dtm081->format(DATE_W3C);
echo $str081_frmt;
$dtz082 = new DateTimeZone('America/Sao_Paulo');
echo '<br><br><br><br>public __construct:<br>';
$dtm081->__construct("now", $dtz082);
echo '$dtm081->__construct("now", $dtz082)';
echo '<br><br>public string format:<br>';
$str081_frmt = $dtm081->format(DATE_W3C);
echo $str081_frmt;
?>
EXERCISE
<?php
$dtm091 = new DateTimeImmutable('America/Sao_Paulo');
$dtz091 = new DateTimeZone('America/Sao_Paulo');
$str091 = '2012-06-05 21:00:00';
$dtm091->__construct($str091, $dtz091);
echo '<br>IPV6 PROTOCOL<br>INITIAL DATE OF OPERATION IN BRAZIL:<br>';
$str091_frmt = $dtm091->format(DATE_W3C);
echo $str091_frmt;
?>
EXERCISE
<?php
date_default_timezone_set('Europe/Oslo');
$tz1 = timezone_open("GMT");
$tz2 = timezone_open("Europe/London");
$tz3 = timezone_open("America/Los_Angeles");
$d = array();
$d[] = date_create("now");
$d[] = date_create("now GMT");
$d[] = date_create("now CET");
$d[] = date_create("now CEST");
$d[] = date_create("now Europe/Oslo");
$d[] = date_create("now America/Los_Angeles");
$d[] = date_create("now", $tz1);
$d[] = date_create("now", $tz2);
$d[] = date_create("now", $tz3);
$d[] = date_create("now GMT", $tz1);
$d[] = date_create("now GMT", $tz2);
$d[] = date_create("now GMT", $tz3);
$d[] = date_create("now Europe/Oslo", $tz1);
$d[] = date_create("now America/Los_Angeles", $tz2);
foreach($d as $date) {
echo $date->format(DateTime::COOKIE), "<br>";
}
?>
EXERCISE
<?php
date_default_timezone_set("GMT");
$micrt = gettimeofday(true);
$d = date_create("now +0000");
echo $d->format('D, d M Y H:i:s T'), "<br><br>";
echo intval($micrt) . '<br><br>';
$d = date_create("@$micrt +0912");
echo $d->format('D, d M Y H:i:s T'), "<br>";
// This code can give wrong results in PHP 7.4.XX
?>
EXERCISE
<?php
date_default_timezone_set('UTC');
if (!defined('PHP_INT_MIN')) {
define('PHP_INT_MIN', intval(-PHP_INT_MAX - 1));
}
$base_time = '28 Feb 2008 12:00:00';
// Most offsets tested in strtotime-relative.phpt. These are tests for dates outside the 32-bit range.
$offsets = array(
// around 10 leap year periods (4000 years) in days
'1460000 days',
'1460969 days',
'1460970 days',
'1460971 days',
'1462970 days',
// around 1 leap year period in years
'398 years',
'399 years',
'400 years',
'401 years',
// around 40000 years
'39755 years',
'39999 years',
'40000 years',
'40001 years',
'41010 years',
// bigger than int (32-bit)
'10000000000 seconds',
'10000000000 minutes',
'10000000000 hours',
'10000000000 days',
'10000000000 months',
'10000000000 years',
);
foreach ($offsets AS $offset) {
foreach (array('+', '-') AS $direction) {
$dt = date_create("$base_time $direction$offset");
echo "$direction$offset: " . date_format($dt, DATE_ISO8601) . "<br><br>";
}
}
?>
EXERCISE
<?php
//Set the default time zone
date_default_timezone_set("Europe/London");
echo "Testing date_create() : basic functionality.<br><br><pre>";
var_dump( date_create() );
echo '<br><br>';
var_dump( date_create("GMT") );
echo '<br><br>';
var_dump( date_create("now") );
echo '<br><br>';
var_dump( date_create("now GMT") );
echo '</pre>';
?>
EXERCISE
<?php
//Set the default time zone
date_default_timezone_set("Europe/London");
echo "Testing date_create() : basic functionality.<br><br>";
function dtcreate($var)
{
$arry14 = (array)( date_create($var) );
foreach($arry14 as $k14 => $v14)
{
echo $k14 . ' => ' . $v14 . '<br>';
}
}
dtcreate("");
echo '<br><br>';
dtcreate("GMT");
echo '<br><br>';
dtcreate("now");
echo '<br><br>';
dtcreate("now GMT");
?>
EXERCISE
<?php
$dtm012 = new DateTimeImmutable('America/New_York');
$dtz012 = new DateTimeZone('America/New_York');
$str012 = 'now';
$dtm012->__construct($str012, $dtz012);
echo '<br>ACTUAL DATE:<br>';
$str012_frmt = $dtm012->format(DATE_RSS);
echo $str012_frmt;
echo '<br><br>' . $dtz012->getName();
echo '<br><br><pre>';
var_dump($dtz012->getLocation());
echo '<br><br>- - - - -<br><br>';
var_dump($dtz012->listAbbreviations());
echo '<br><br>- - - - -<br><br>';
var_dump($dtz012->listIdentifiers());
echo '</pre>';
?>