Example
<?php
echo date_default_timezone_get();
?>
Output
Asia/Calcutta
Example
<?php
echo date_default_timezone_get();
?>
Output
Asia/Calcutta
<?php
$date = new DateTime();
$date->setDate(2011, 5, 17);
echo $date->format(‘Y-m-d’);
?>
echo $date->format(‘Y-m-d’);
?>
Output
2011-05-17
Its an alias of DateTime::setDate() and sets the date.
Example
<?php
$date = date_create();
date_date_set($date, 2011, 5, 17);
echo date_format($date, ‘Y-m-d’);
?>
echo $date->format(‘Y-m-d’);
?>
Output
2011-05-17
Example
<?php
try {
$date = new DateTime(‘2008-05-17’);
} catch (Exception $e) {
echo $e->getMessage();
exit(1);
}
echo $date->format(‘Y-m-d’);
?>
Output
2007-05-15
<?php
$date = date_create(‘2007-5-17’);
if (!$date) {
$e = date_get_last_errors();
foreach ($e[‘errors’] as $error) {
echo “$errorn”;
}
exit(1);
}
echo date_format($date, ‘Y-m-d’);
?>
Output
2007-05-15
<?php
$date = DateTime::createFromFormat(‘j-M-Y’, ’17-May-2012′);
echo $date->format(‘Y-m-d’);
?>
Output
2012-05-17
Example
<?php
$date = date_create_from_format(‘j-M-Y’, ’17-May-2012′);
echo date_format($date, ‘Y-m-d’);
?>
Output
2012-05-17
We can add number of of days, months, years, hours and seconds with a DateTime object.
Example
Output
2012-05-05
Example
<?php
$date = new DateTime(“24-May-2011 20:15:22”);
echo $date->format(“d-m-Y H:i:s”).'<br />’;
date_add($date, new DateInterval(“P1Y”));
echo ‘<br />’.$date->format(“d-m-Y”).’ : I Year’;
?>
Output
24-05-2011 20:15:22
24-05-2012 : I Year
It checks whether the date is valid or not
General Format :checkdate ( int $month , int $day , int $year )
Example :
<?php
if(checkdate(12, 31, 2000))
echo “Given Date Is Correct”;
?>
Output
Given Date Is Correct