Calculate the hours between two dates in PHP.

Marc Wagner, October 26, 2022

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.

Avatar of Marc Wagner
Marc Wagner

Hi Marc here. I'm the founder of Forge12 Interactive and have been passionate about building websites, online stores, applications and SaaS solutions for businesses for over 20 years. Before founding the company, I already worked in publicly listed companies and acquired all kinds of knowledge. Now I want to pass this knowledge on to my customers.

Similar Topics

Comments

Leave A Comment