- date_diff: documentation ( source)
- date_create: documentation ( source)
- round: documentation ( source)
- date: documentation ( source)
- strtotime: documentation ( source)
<?php
function dateTimeDiff($start, $end, $units = 'Days')
{
$start_ts = strtotime($start);
$end_ts = strtotime($end);
$diff = $end_ts - $start_ts;
$divider = 86400;
if ($units != 'Days') {
$divider = $divider / 24;
if ($units != 'Hours') {
$divider = $divider / 60;
if ($units != 'Minutes') {
$divider = $divider / 60;
}
}
}
return round($diff / $divider);
}
echo "\n our function: ";
echo dateTimeDiff(date('Y-m-d'), '13-11-2013');
$datetime1 = date_create('now');
$datetime2 = date_create('13-11-2013');
$interval = date_diff($datetime1, $datetime2);
echo "\n php function: ";
echo $interval->format('%R%a dias');