note 106160 deleted from function.time by cmb
| From: | cmb@php.net | Date: | Sun, 01 Aug 2021 14:55:47 +0000 |
| Subject: | note 106160 deleted from function.time by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-216706@lists.php.net to get a copy of this message | ||
Note Submitter: instantcarma2000 at yahoo dot se
----
This is a very basic and simple function to display either min -OR- hrs and min -OR- days and hrs
-OR- days depending on how many seconds are given.
Example use:
<?php
$td = time_diff($timestamp1-$timestamp2); // has to be a positive result
$td .= ($td=="now")? "":" ago"; // in this example "ago"
echo $td;
function time_diff($s){
$m=0;$hr=0;$d=0;$td="now";
if($s>59) {
$m = (int)($s/60);
$s = $s-($m*60); // sec left over
$td = "$m min";
}
if($m>59){
$hr = (int)($m/60);
$m = $m-($hr*60); // min left over
$td = "$hr hr"; if($hr>1) $td .= "s";
if($m>0) $td .= ", $m min";
}
if($hr>23){
$d = (int)($hr/24);
$hr = $hr-($d*24); // hr left over
$td = "$d day"; if($d>1) $td .= "s";
if($d<3){
if($hr>0) $td .= ", $hr hr"; if($hr>1) $td .= "s";
}
}
return $td;
}
?>