很难理解如何在PHP中操作日期和时间

I'm really not grasping how dates and times get formatted in PHP for use in mathematical equations. My goal is this;

Get a date and time from the database;

// Get array for times in
$sth = mysqli_query($conn,"SELECT * FROM ledger ORDER BY ID");
$timeins = array();
while($r = mysqli_fetch_assoc($sth)) {
    $timeins[] = $r["timein"];
    //OR
    array_push($timeins, $r['timein']);
}

And then find the distance between the current time and the variable in the array, $timeins[0], and then put the minutes, hours, and days difference in separate simple variables for later use. These variables will be used on their own in if statements to find out if the person has passed certain amounts of time.

edit: the format of the dates being returned from the DB is in the default TIMESTAMP format for MySQL. E.g. 2018-08-06 17:38:37.

Using the DateTime Class in php is the best way to get accurate results.

$dateNow = new DateTime('now');
$dateIn = DateTime::createFromFormat('d-m-Y H:i:s',  $timeins[0]);

$interval = $dateNow->diff($dateIn);

echo $interval->format('%d days, %h hours, %i minutes, %s seconds');
$deltaDays = $interval->d;
$deltaHours = $interval->h;
...

You have to make sure the input format for you DB date is correct, in this case, I assumed d-m-y H:i:s, and then you can output in any format you need as well, as shown in the date docs: http://php.net/manual/en/function.date.php

It is also possible to perform datetime operations in SQL, to get a difference between two datetime/timestamp values in days, hours, minutes... We can use expressions in the SELECT list, to return the results as columns in the resultset.

Ditching the SELECT * pattern, and specifying an explicit list of expressions that we need returned:

$sql = "
SELECT t.id 
     , t.timein 
     , TIMESTAMPDIFF(DAY    ,t.timein,NOW()) AS diff_days
     , TIMESTAMPDIFF(HOUR   ,t.timein,NOW()) AS diff_hours
     , TIMESTAMPDIFF(MINUTE ,t.timein,NOW()) AS diff_minute
  FROM ledger t
 ORDER BY t.id ";

if( $sth = mysqli_query($conn,$sql) ) {
  // execution successful 
  ...
} else {
  // handle sql error
}

You should use the DateTime class in PHP to do any date manipulation. You can convert a string representation of a MySQL format time to a PHP DateTime object using

$date = DateTime::createFromFormat('Y-m-d H:i:s', $mysqldate);

Also you can create a DateTime object representing the current time using the constructor with no argument:

$now = new DateTime();

To get the difference between two dates as a DateInterval object, use the builtin diff method:

$diff = $now->diff($date);

As a complete example:

$now = new DateTime();
$mysqldate = '2018-04-03 12:30:01';
$date = DateTime::createFromFormat('Y-m-d H:i:s', $mysqldate);
$diff = $now->diff($date);
$diff_days = (int)$diff->format('%a');
$diff_hours = $diff->h;
$diff_minutes = $diff->m;
echo "$diff_days days, $diff_hours hours, $diff_minutes minutes";

Output:

125 days, 9 hours, 4 minutes

Note that you have to use $diff->format('%a') rather than $diff->d to get the days between two dates, as $diff->d will not include the days in any months between the two dates (in this example it will return 3 for today being August 6).