I was learning PHP recently, and a friend asked about time calculation. At this time, I thought of the calculation functions of delphi and mssql. They are very convenient to use, but I checked the PHP manual and found no similar time calculation. Function, through inspiration from online articles and own testing, I still find a simple way to implement it The details are as follows:
1> If we know the start time, and we need to add or subtract a time to get a result time, we can use the following code
$time1=2008-10-1 12:30:30;
echo date(Y-m-d H:i:s,strtotime($time1)+30*60);//Pay attention to the case in quotation marks, the minute is i not m
Execution result: 2008-10-01 13 :00:30
2>If we want to calculate the difference between two times, we can use the following method:
$time1=2008-10-1 12:30:30;
$time2=2008- 10-1 13:45:30;
$diff=(strtotime($time2)-strtotime($time1))/60;
echo The time difference between $time1. and $time2..$diff.minutes ;
Execution result: The time difference from 2008-10-1 12:30:30 to 2008-10-1 13:45:30 is 75 minutes
Summary: The basis of PHP time calculation is seconds, grasp this After setting the rules, you can convert the time difference into minutes, hours, etc., making time calculation very simple and easy.