I was learning PHP recently, and a friend asked me about the calculation of time. 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 functions. Through the online article For inspiration and your own testing, you can still find a simple way to achieve it
as follows:
1>If we know the start time and want 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('Ymd H:i:s',strtotime($time1)+30*60);//Pay attention to the case within the 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 $time1.'The time difference from '.$time2.'.$diff.'minutes';
Execution results: The time difference is 75 minutes from 2008-10-1 12:30:30 to 2008-10-1 13:45:30 Summary: The basis of PHP time calculation is seconds. After grasping this rule, the time difference can be converted into Minutes, hours, etc. can be used, making time calculation very simple and easy.