<?php
$datetime1 = new DateTime(‘2009-5-17’);
$datetime2 = new DateTime(‘2012-5-17’);
$interval = $datetime1->diff($datetime2);
echo $interval->format(‘%R%a days’);
?>
Output
+1096 days
<?php
$datetime1 = new DateTime(‘2009-5-17’);
$datetime2 = new DateTime(‘2012-5-17’);
$interval = $datetime1->diff($datetime2);
echo $interval->format(‘%R%a days’);
?>
Output
+1096 days
<?php
$date1 = date_create(‘2009-5-17’);
$date2 = date_create(‘2012-5-17’);
$interval = date_diff($date1, $date2);
echo $interval->format(‘%R%a days’);
?>
Output
+1096 days
Example
<?php
date_default_timezone_set(‘Asia/Culcutta’);
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