Time Difference
Usage: $this->time->dateDiff('2012-01-19 08:30:41','2012-01-19 06:36:42')

@param type $d1
@param type $d2
@return string
541146
function dateDiff($d1, $d2)
    {
        $date1 = strtotime($d1);
        $date2 = strtotime($d2);
        $seconds = $date1 - $date2;
        $weeks = floor($seconds / 604800);
        $seconds -= $weeks * 604800;
        $days = floor($seconds / 86400);
        $seconds -= $days * 86400;
        $hours = floor($seconds / 3600);
        $seconds -= $hours * 3600;
        $minutes = floor($seconds / 60);
        $seconds -= $minutes * 60;
        $months = round(($date1 - $date2) / 60 / 60 / 24 / 30);
        $years = round(($date1 - $date2) / (60 * 60 * 24 * 365));

        $secs_unit = (abs($seconds) == 1) ? ' Second ' : ' Seconds ';
        $mins_unit = (abs($minutes) == 1) ? ' Minute ' : ' Minutes ';
        $hours_unit = (abs($hours) == 1) ? ' Hour ' : ' Hours ';
        $days_unit = (abs($days) == 1) ? ' Day ' : ' Days ';
        $weeks_unit = (abs($weeks) == 1) ? ' Week ' : ' Weeks ';
        $months_unit = (abs($months) == 1) ? ' Month ' : ' Months ';
        $years_unit = (abs($years) == 1) ? ' Year ' : ' Years ';

        $difference = "";

        if (!empty($years))
            $difference .= $years . $years_unit;
        if (!empty($months))
            $difference .= $months . $months_unit;
        if (!empty($weeks))
            $difference .= $weeks . $weeks_unit;
        if (!empty($days))
            $difference .= $days . $days_unit;
        if (!empty($hours))
            $difference .= $hours . $hours_unit;
        if (!empty($minutes))
            $difference .= $minutes . $mins_unit;
        if (!empty($seconds))
            $difference .= $seconds . $secs_unit;

        return $difference;
    }
705380
public function getDifferenceInDays($compare = null)
        {
            if (!$compare) {
                $compare = new Date(null, $this->getTimezone());
            }

            return $this->diff($compare)->format('%r%a');
        }
1172711
public static function DateRangeDiff(string $from, string $to)
    {
        $date_Range = new \Phunc\DateRange\DateRange();
        $date_Range->fillFromString($from, $to);
        $date_Range->getFrom()->hour(0)->minute(0)->second(0);
        $date_Range->getTo()->hour(24); //->minute(59)->second(59);
        return $date_Range->diffInDays();
    }
1220535
private function isLessThan59Days23Hours59Mins29Secs($timeDifference)
    {
        return $timeDifference >= (
                ($this->secondsPerDay * 29) +
                ($this->secondsPerHour * 23) +
                ($this->secondsPerMinute * 59) +
                30
            )
            &&
            $timeDifference <= (
                ($this->secondsPerDay * 59) +
                ($this->secondsPerHour * 23) +
                ($this->secondsPerMinute * 59) +
                29
            );
    }
764533
public static function diff(DateTime $time,DateTime $time0=null)
    {
        if($time0==null){
            $time0=new DateTime();
        }
        return $time->diffFrom($time0);
    }
230955
public static function diffDates($date1, $date2)
    {
        $ts1 = strtotime($date1);
        $ts2 = strtotime($date2);
        
        $seconds_diff = $ts2 - $ts1;
        
        return floor($seconds_diff / 3600 / 24);
    }
803765
public static function getAgeDays(Date $d1, Date $d2): int
    {
        if ($d2->maximumJulianDay() >= $d1->minimumJulianDay() && $d2->minimumJulianDay() <= $d1->maximumJulianDay()) {
            // Overlapping dates
            $jd = $d1->minimumJulianDay();
        } else {
            // Non-overlapping dates
            $jd = $d2->minimumJulianDay();
        }

        // Days - integer only (for sorting, rather than for display)
        if ($jd && $d1->minimumJulianDay()) {
            return $jd - $d1->minimumJulianDay();
        }

        return -1;
    }
1336830
public static Expression dateDiffMillis(Expression expression1, Expression expression2, DatePart part) {
        return x("DATE_DIFF_MILLIS(" + expression1.toString() + ", " + expression2.toString()
                + ", \"" + part.toString() + "\")");
    }
1006386
public static function dateDiff($Date1, $Date2)
    {
        $DateList1=explode("-", $Date1);
        $DateList2=explode("-", $Date2);
        $d1=mktime(0, 0, 0, $DateList1[1], $DateList1[2], $DateList1[0]);
        $d2=mktime(0, 0, 0, $DateList2[1], $DateList2[2], $DateList2[0]);
        $Days=round(($d1-$d2)/3600/24);
        return $Days;
    }
795606
public static function FindClockDiff() {
			$time = time();
			$pong = self::Ping();

			return ( $time - strtotime( $pong->timestamp ) );
		}
470010
public static function compareDatetime($a, $b)
    {
        $timea = strtotime($a);
        $timeb = strtotime($b);

        if ($timea === false || $timeb === false) {
            return false;
        }

        if ($timea > $timeb) {
            return 1;
        } elseif ($timea < $timeb) {
            return -1;
        }

        return 0;
    }
994210
public function timer_diff()
    {
        $PAGELOADTIME = microtime();
        $PAGELOADTIME = explode(' ', $PAGELOADTIME);
        $PAGELOADTIME = $PAGELOADTIME[1] + $PAGELOADTIME[0];
        $now = $PAGELOADTIME;
        $TIMEDIFF = ($now - $this->basetime);

        return $TIMEDIFF;
    }
346924
@SuppressWarnings("deprecation")
	public static boolean dateTimeEquals(java.util.Date d1, java.util.Date d2) {
        if (d1 == null || d2 == null) {
            return false;
        }

        return d1.getDate() == d2.getDate()
                && d1.getMonth() == d2.getMonth()
                && d1.getYear() == d2.getYear()
                && d1.getHours() == d2.getHours()
                && d1.getMinutes() == d2.getMinutes()
                && d1.getSeconds() == d2.getSeconds();
    }
212265
public static function dateDiff($interval, $startDateTime, $endDateTime)
    {
        $diff = static::getTimestamp($endDateTime) - static::getTimestamp($startDateTime);
        switch ($interval) {
            case 'Y' : // 年
                $result = bcdiv($diff, 60 * 60 * 24 * 365);
                break;
            case 'M' : // 月
                $result = bcdiv($diff, 60 * 60 * 24 * 30);
                break;
            case 'W' : // 星期
                $result = bcdiv($diff, 60 * 60 * 24 * 7);
                break;
            case 'D' : // 日
                $result = bcdiv($diff, 60 * 60 * 24);
                break;
            case 'H' : // 时
                $result = bcdiv($diff, 60 * 60);
                break;
            case 'N' : // 分
                $result = bcdiv($diff, 60);
                break;
            case 'S' : // 秒
            default :
                $result = $diff;
                break;
        }
        return $result;
    }
250190
public static function compare($date1, $date2, $operator = null)
    {
        $date1 = $date1 instanceof \DateTimeInterface ? $date1 : new \DateTime($date1);
        $date2 = $date2 instanceof \DateTimeInterface ? $date2 : new \DateTime($date2);

        if ($operator === null) {
            if ($date1 == $date2) {
                return 0;
            }

            return (int) ($date1 < $date2);
        }

        switch ((string) $operator) {
            case '=':
                return $date1 == $date2;
            case '!=':
                return $date1 != $date2;
            case '>':
            case 'gt':
                return $date1 > $date2;
            case '>=':
            case 'gte':
                return $date1 >= $date2;
            case '<':
            case 'lt':
                return $date1 < $date2;
            case '<=':
            case 'lte':
                return $date1 <= $date2;
        }

        throw new \InvalidArgumentException('Invalid operator: ' . $operator);
    }
1085501
static function convertInterval(DateInterval $d) {
		$i = 0;
		$i += $d->s;
		$i += $d->i * self::MINUTE;
		$i += $d->h * self::HOUR;
		$i += $d->d * self::DAY;
		$i += $d->m * self::MONTH;
		$i += $d->y * self::YEAR;

		return $i;
	}
663405
final public static function formatDateDiff($start, $end = null, $parts = 0)
    {
        if (!($start instanceof \DateTimeInterface)) {
            $start = self::asDateTime($start);
        }

        if ($end === null) {
            $end = self::now();
        } else {
            if (!($end instanceof \DateTimeInterface)) {
                $end = self::asDateTime($end);
            }
        }

        $interval = $end->diff($start);

        return self::formatInterval($interval, $parts);
    }