如何通过其中一行订购MySQL?

With a simple mysqli_query I retreive all tasks from my database. Every single task has a deadline. I want to calculate how many days are left for each single task. When that's done, I'd like to order all the tasks based on the days left. How can I achieve this? I have this simple piece of code getting all the tasks:

$sql = mysqli_query($mysqli, "SELECT * FROM tasks");
while($rows=mysqli_fetch_array($sql,MYSQLI_ASSOC)){
   $deadline = $rows['deadline'];
   $today = strtotime('now');
   $date_diff = $deadline - $today;

   echo $date_diff;
}

But how can I order them by $date_diff?

You can perform this minus operation in mysql and ORDER BY the same. Here is how you do it,

$sql = mysqli_query($mysqli, "SELECT * FROM tasks ORDER BY (deadline-UNIX_TIMESTAMP())") DESC;

We've used UNIX_TIMESTAMP() to get the current timestamp and deducting it from the deadline.

At the end of the query we've used DESC keyword which will sort the result by days left in descending order.

You can simply do that in you sql query.

$sql = mysqli_query($mysqli, "SELECT TIMEDIFF(deadline, CURRENT_TIME) as date_diff FROM tasks ORDER BY TIMEDIFF(deadline, CURRENT_TIME)");