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
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
<?php
date(“M”, mktime(0, 0, 0,MONTH_NUMBER,DATE, YEAR));
eg: echo date(“M”, mktime(0, 0, 0,5,17, 1984)); // May
?>
<?php $startDate=explode("-", "02-08-2010"); $endDate=explode("-", "26-04-2007"); echo gregoriantojd($startDate[1], $startDate[0], $startDate[2]) - gregoriantojd($endDate[1], $endDate[0], $endDate[2]); //1194 ?>