PHP time subtraction

I have been searching for the answer for several hours but cannot find it.

I am writing a simple script . The user sets the start and end times. So, for example, someone works from 8:00 to 16:00. How can I subtract this time to find out how long a person has been working?

I experimented with strtotime(); but without success ...

+7
source share
3 answers

A little prettier:

 $ a = new DateTime ('08: 00 ');
 $ b = new DateTime ('16: 00 ');
 $ interval = $ a-> diff ($ b);

 echo $ interval-> format ("% H");

This will give you the difference in hours.

+25
source

If you get the correct date strings, you can use this:

 $workingHours = (strtotime($end) - strtotime($start)) / 3600; 

This will give you a person’s hours of work.

+9
source

Another solution would be to go through the difference in integer values ​​of Unix-timestamp (in seconds).

 <?php $start = strtotime('10-09-2019 12:01:00'); $end = strtotime('12-09-2019 13:16:00'); $hours = intval(($end - $start)/3600); echo $hours.' hours'; //in hours //If you want it in minutes, you can divide the difference by 60 instead $mins = (int)(($end - $start) / 60); echo $mins.' minutues'.'<br>'; ?> 

This solution would be better if source dates are stored in Unix-timestamp format.

0
source

Source: https://habr.com/ru/post/1345601/


All Articles