Calculate the hours between two dates in PHP.

Marc Wag­ner

Octo­ber 26, 2022

3 min read|

Today I’ll show you how to use PHP to cal­cu­la­te the num­ber of hours bet­ween two dates.

The PHP class #

First, I wro­te a class that can accept the two dates. In addi­ti­on to this, you can optio­nal­ly spe­ci­fy a num­ber of hours. This num­ber of hours is used if a fixed num­ber of hours per day is always to be retur­ned. If you do not spe­ci­fy the para­me­ter, 24 hours per day will be cal­cu­la­ted auto­ma­ti­cal­ly.

class CompareDate {
	/**
	 * @var false|int
	 */
	private $_dateEnd;
	/**
	 * @var false|int
	 */
	private $_dateStart;
	/**
	 * @var int
	 */
	private $_hoursByDay;

	public function __construct( string $dateStart, string $dateEnd, int $hoursByDay = 0 ) {
		$this->setDateStart( $dateStart );
		$this->setDateEnd( $dateEnd );
		$this->setHoursByDay( $hoursByDay );
	}

	private function setDateStart( string $date ) {
		$this->_dateStart = strtotime( $date );
	}

	private function setDateEnd( string $date ) {
		$this->_dateEnd = strtotime( $date );
	}

	private function setHoursByDay( int $hours ) {
		$this->_hoursByDay = $hours;
	}

	private function isTimeSet() {
		$hourStart   = date( "H", $this->_dateStart );
		$hourEnd     = date( "H", $this->_dateEnd );
		$minuteStart = date( "i", $this->_dateStart );
		$minuteEnd   = date( "i", $this->_dateEnd );

		if ( ( $hourStart == 0 && $hourEnd == 0 && $minuteStart == 0 && $minuteEnd == 0 ) || $this->_hoursByDay != 0 ) {
			return false;
		}

		return true;
	}

	private function getHoursByDay( int $default = 0 ) {
		if ( $default != 0 && $this->_hoursByDay == 0 ) {
			return $default;
		}

		return $this->_hoursByDay;
	}

	public function getDiffHours() {
		if ( $this->isTimeSet() ) {
			$days  = $this->getDiffDays();
			$start = $this->getDateStart();
			$end   = $this->getDateEnd();

			$DateTimeStart = new DateTime( $start );
			$DateTimeEnd   = new DateTime( $end );

			$hours = $days * 24;
			$hours += $DateTimeStart->diff( $DateTimeEnd )->h;

			return $hours;
		} else {
			$days = $this->getDiffDays( true );
			$hours = $this->getHoursByDay( 24 );

			return $days * $hours;
		}
	}

	private function getFormattedDate( string $date ): string {
		return date( 'd.m.Y H:i', $date );
	}

	private function getDateStart(): string {
		return $this->getFormattedDate( $this->_dateStart );
	}

	private function getDateEnd(): string {
		return $this->getFormattedDate( $this->_dateEnd );
	}

	public function getDiffDays( bool $includeToday = false ): int {
		$start = $this->getDateStart();
		$end   = $this->getDateEnd();

		$DateTimeStart = new DateTime( $start );
		$DateTimeEnd   = new DateTime( $end );

		$days = $DateTimeStart->diff( $DateTimeEnd )->days;
		if ( $includeToday ) {
			$days += 1;
		}

		return $days;
	}
}

This is how you use the class. #

To per­form a cal­cu­la­ti­on now, you have the fol­lo­wing opti­ons:

Cal­cu­la­te days bet­ween two dates with PHP:

$DateObject = new CompareDate('20.03.2020', '21.03.2020', 8);
echo $DateObject->getDiffDays();
// Output: 1
echo $DateObject->getDiffDays(true);
// Output: 2 - zählt ebenfalls den aktuellen Tag.

Cal­cu­la­te hours bet­ween two dates with PHP:

$DateObject = new CompareDate('20.03.2020', '20.03.2020');
echo $DateObject->getDiffHours();
// Output: 24 - Falls keine Stunden angegeben wird jeder Tag mit 24 berechnet.

$DateObject = new CompareDate('20.03.2020', '20.03.2020', 8);
echo $DateObject->getDiffHours();
// Output: 8 - Falls keine Stunden angegeben wird jeder Tag mit 24 berechnet.

$DateObject = new CompareDate('20.03.2020 16:00', '20.03.2020 17:00');
echo $DateObject->getDiffHours();
// Output: 1

That’s about it. With the class you can:

  • Deter­mi­ne the days bet­ween two dates with PHP.
  • Deter­mi­ne the hours bet­ween two dates with PHP.
  • Cal­cu­la­te a fixed num­ber of hours based on the spe­ci­fied days.
  • Cal­cu­la­te a time inter­val bet­ween two clock times.

I hope this litt­le script will help you with your web deve­lo­p­ment. If you have any ques­ti­ons, feel free to get in touch. Of cour­se, you can also lea­ve a com­ment with your feed­back.

88e86fcb816eff22bc917094df2862d8dd5c0e978b333e6dd5f36f808990c261 96

Arti­kel von:

Marc Wag­ner

Hi Marc here. I’m the foun­der of Forge12 Inter­ac­ti­ve and have been pas­sio­na­te about buil­ding web­sites, online stores, appli­ca­ti­ons and SaaS solu­ti­ons for busi­nesses for over 20 years. Befo­re foun­ding the com­pa­ny, I alre­a­dy work­ed in publicly lis­ted com­pa­nies and acqui­red all kinds of know­ledge. Now I want to pass this know­ledge on to my cus­to­mers.

Hast du eine Fra­ge? Hin­ter­lass bit­te einen Kom­men­tar