note 106160 modified in function.time by danbrown

From: Date: Fri, 14 Oct 2011 22:39:19 +0000
Subject: note 106160 modified in function.time by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-183893@lists.php.net to get a copy of this message
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; } ?> --was-- 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: $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; } http://php.net/manual/en/function.time.php

« previous php.notes (#183893) next »