Hướng dẫn php subtract time

I have been looking for an answer for a few hours now, but I can't find one.

I'm writing a simple script. The user sets their work start and end time. So, for example, somebody is working from 8:00 to 16:00. How can I subtract this time to see how long the person has been working?

I was experimenting with strtotime(); but without success...

Hướng dẫn php subtract time

CyanCoding

1,0181 gold badge14 silver badges33 bronze badges

asked Mar 28, 2011 at 18:42

A bit nicer is the following:

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

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

That will give you the difference in hours.

answered Mar 28, 2011 at 18:51

fabfab

1,82915 silver badges21 bronze badges

1

If you get valid date strings, you can use this:

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

This will give you the hours a person has been working.

answered Mar 28, 2011 at 18:47

jm_toballjm_toball

1,1977 silver badges10 bronze badges

2

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

';
?>

This solution would be a better one if your original dates are stored in Unix-timestamp format.

N69S

13.9k3 gold badges18 silver badges33 bronze badges

answered Sep 23, 2019 at 14:23

1

Subtract time (hours, minutes, and seconds) from date can be easily done using the date() and strtotime() function in PHP. The date() function formats a local date and time, and returns a formatted string. On the other hand, the strtotime() function converts string formatted DateTime into Unix timestamp. You can use these two functions together to subtract time form the current date using PHP.

The following example code helps you to subtract time from current DateTime or a specific date-time.

Subtract Hours from Datetime
Use the following code snippet to subtract 2 hours from current date and time using PHP.

// Current date and time
$datetime date("Y-m-d H:i:s");// Convert datetime to Unix timestamp
$timestamp strtotime($datetime);// Subtract time from datetime
$time $timestamp - (60 60);// Date and time after subtraction
$datetime date("Y-m-d H:i:s"$time);

Subtract Minutes from Datetime
Use the following code snippet to subtract 5 minutes from current date and time using PHP.

// Current date and time
$datetime date("Y-m-d H:i:s");// Convert datetime to Unix timestamp
$timestamp strtotime($datetime);// Subtract time from datetime
$time $timestamp - (60);// Date and time after subtraction
$datetime date("Y-m-d H:i:s"$time);

Subtract Seconds from Datetime
Use the following code snippet to subtract 30 seconds from current date and time using PHP.

// Current date and time
$datetime date("Y-m-d H:i:s");// Convert datetime to Unix timestamp
$timestamp strtotime($datetime);// Subtract time from datetime
$time $timestamp 30;// Date and time after subtraction
$datetime date("Y-m-d H:i:s"$time);

How to time minus in PHP?

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.

How to subtract hours from date PHP?

Example 1: PHP Subtract Hours to DateTime.

index.php. $date = "2021-11-01 10:10:05"; $newDate = date('Y-m-d H:i:s', strtotime($date. ... .

Output: 2021-11-01 07:10:05..

index.php. $newDate = date('Y-m-d H:i:s', strtotime(' -3 hours')); echo $newDate; ... .

Output: Read Also: How to Add Seconds to Time in PHP? 2021-11-10 03:49:33..

How do you subtract minutes from a timestamp?

Subtract minutes from a timestamp in Python using timedelta.

Step 1: If the given timestamp is in a string format, then we need to convert it to the datetime object..

Step 2: Create an object of timedelta, to represent an interval of N minutes. ... .

Step 3: Subtract the timedelta object from the datetime object in step 1..