Tag Archives: Date

DateTime::getLastErrors()

It returns the warnings and errors .
Example

<?php
echo “<pre>”;
try {
$date = new DateTime(‘phpcodez’);
} catch (Exception $e) {
print_r(DateTime::getLastErrors());
}
?>

Output

Array
(
[warning_count] => 1
[warnings] => Array
(
[6] => Double timezone specification
)

[error_count] => 1
[errors] => Array
(
[0] => The timezone could not be found in the database
)

)

date_get_last_errors

It returns the warnings and errors and is an alias of DateTime::getLastErrors().
Example

<?php
echo “<pre>”;
$date = date_create(‘phpcodez’);
print_r(date_get_last_errors());
?>

Output

Array
(
[warning_count] => 1
[warnings] => Array
(
[6] => Double timezone specification
)

[error_count] => 1
[errors] => Array
(
[0] => The timezone could not be found in the database
)

)

DateTime::diff()

It can be used to find out the  difference between two dates
Example

<?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

date_diff

It is an alias of DateTime::diff()  and can be used to find out the  difference between two dates
Example

<?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