Calculate the hours between two dates in PHP.

Marc Wagner, October 26, 2022

Today I’ll show you how to use PHP to calculate the number of hours between two dates.

The PHP class #

First, I wrote a class that can accept the two dates. In addition to this, you can optionally specify a number of hours. This number of hours is used if a fixed number of hours per day is always to be returned. If you do not specify the parameter, 24 hours per day will be calculated automatically.

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 perform a calculation now, you have the following options:

Calculate days between 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.

Calculate hours between 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:

  • Determine the days between two dates with PHP.
  • Determine the hours between two dates with PHP.
  • Calculate a fixed number of hours based on the specified days.
  • Calculate a time interval between two clock times.

I hope this little script will help you with your web development. If you have any questions, feel free to get in touch. Of course, you can also leave a comment with your feedback.

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