Home > Article > Backend Development > Summary of PHP time calculation problems_PHP tutorial
The details are 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('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 $ Time difference from time1.' to '.$time2.''.$diff.'minute';
Execution result: Time difference from 2008-10-1 12:30:30 to 2008-10-1 13:45:30 75 minutes
Summary: The basis of PHP time calculation is seconds. After grasping this rule, you can convert the time difference into minutes, hours, etc., making time calculation very simple and easy.