It is not possible in Mysql to do calculation with values in other records. This is a limitation of Mysql, not SQL.
If you wish to retrieve data sorted with respect to a certain column, you can add an order by clause so there is no need for a sort algorithm.
It is a bit unclear what you mean by
Do you mean next with respect to the order in which records were entered or just the one with the next lowest value? If the latter you can do that sorting in the select statement
select * from t order by name,time desc
if you retrieve the data in this order (names sorted ascending and time values sorted descendingly for each name) you can do the calculation in PHP, keeping track of when the name column changes value.
if the former, you need a timestamp column so you can order by this column instead. A relational database will not keep track of the order in which records are entered.