cordoprod Posted May 22, 2008 Share Posted May 22, 2008 Hi. I have a function to show time elapsed from a timestamp out of time(); Here is my functions: // function to generate time elapsed function time_elapsed($startTime, $endTime = false) { $startTime = ctype_digit($startTime) ? $startTime : strtotime($startTime); $endTime = $endTime ? (ctype_digit($endTime) ? $endTime : strtotime($endTime)) : time(); if ($endTime > $startTime) { $diff = $endTime - $startTime; return array('år' => gmdate('Y', $diff) - 1970, 'måneder' => gmdate('m', $diff) - 1, 'dager' => gmdate('d', $diff) - 1, 'timer' => gmdate('H', $diff), 'minutter' => gmdate('i', $diff), 'sekunder' => gmdate('s', $diff)); } else { } } // function to get it function time_elapsed_done($timestamp) { $time = time_elapsed($timestamp); foreach($time as $unit => $value) { if($unit == 'minutter' && $value == 1) $unit = "minutt"; if($unit == 'timer' && $value == 1) $unit = "time"; if($unit == 'dager' && $value == 1) $unit = "dag"; if($unit == 'år' && $value == 1) $unit = "år"; if($value < 60 and $unit == 'sekunder') { $value = "få sekunder"; $unit = "siden"; } if ($value == 0) continue; if ($unit == 'sekunder') continue; echo ltrim($value, '0'), ' ', (($value == 1) ? $unit : $unit), ' '; } } What i wanna do is to make it more advanced. Here's some things i wanna change: If the unit is e.g 1 day and 6 hours, don't show minutes Add commas between units e.g 1 hour, 2 minutes If the unit is only 0 seconds, show seconds Please help =) Link to comment https://forums.phpfreaks.com/topic/106821-solved-time-elapsed-help/ Share on other sites More sharing options...
cordoprod Posted May 22, 2008 Author Share Posted May 22, 2008 Any ideas? Link to comment https://forums.phpfreaks.com/topic/106821-solved-time-elapsed-help/#findComment-547634 Share on other sites More sharing options...
thebadbad Posted May 23, 2008 Share Posted May 23, 2008 I see you're using some code I posted earlier here, cool I've written some neat functions to deal with this: <?php function putTimeArray($startTime, $endTime = false) { $startTime = ctype_digit($startTime) ? $startTime : strtotime($startTime); $endTime = $endTime ? (ctype_digit($endTime) ? $endTime : strtotime($endTime)) : time(); if ($endTime > $startTime) { $diff = $endTime - $startTime; $y = gmdate('Y', $diff) - 1970; $mo = gmdate('n', $diff) - 1; $d = gmdate('j', $diff) - 1; $h = gmdate('G', $diff); $mi = ltrim(gmdate('i', $diff), '0'); $s = ltrim(gmdate('s', $diff), '0'); return array( 'y' => array($y, ($y == 1) ? 'year' : 'years'), 'mo' => array($mo, ($mo == 1) ? 'month' : 'months'), 'd' => array($d, ($d == 1) ? 'day' : 'days'), 'h' => array($h, ($h == 1) ? 'hour' : 'hours'), 'mi' => array($mi, ($mi == 1) ? 'minute' : 'minutes'), 's' => array($s, ($s == 1) ? 'second' : 'seconds') ); } else { die('Error: endTime parameter must be larger than startTime parameter in putTimeArray function (part of timeDifference class).'); } } function getTimeInWords ($t) { switch (true) { // years and months case $t['y'][0] > 0 && $t['mo'][0] > 0: return "{$t['y'][0]} {$t['y'][1]} and {$t['mo'][0]} {$t['mo'][1]} ago"; case $t['y'][0] > 0 && $t['mo'][0] == 0: return "{$t['y'][0]} {$t['y'][1]}"; // months and days case $t['mo'][0] > 0 && $t['d'][0] > 0: return "{$t['mo'][0]} {$t['mo'][1]} and {$t['d'][0]} {$t['d'][1]} ago"; case $t['mo'][0] > 0 && $t['d'][0] == 0: return "{$t['mo'][0]} {$t['mo'][1]} ago"; // days case $t['d'][0] > 0: return "{$t['d'][0]} {$t['d'][1]} ago"; // hours case $t['h'][0] > 0: return "{$t['h'][0]} {$t['h'][1]} ago"; // minutes case $t['mi'][0] > 0: return "{$t['mi'][0]} {$t['mi'][1]} ago"; // seconds case $t['s'][0] > 10: return 'A moment ago'; default: return 'Just now'; } } // usage $t = putTimeArray('2008-02-03 12:00:00'); echo getTimeInWords($t); ?> Try them out! Change the date/time given in putTimeArray to see what it outputs, if you can't read the getTimeInWords function. Hopefully you can use it as inspiration to achieve what you want. (Actually I wrote these functions as part of a PHP class, just ask if you want it). Link to comment https://forums.phpfreaks.com/topic/106821-solved-time-elapsed-help/#findComment-548067 Share on other sites More sharing options...
cordoprod Posted May 24, 2008 Author Share Posted May 24, 2008 Thanks ! But does it comes with commas? e.g 1 hour, 1 minute? Besides of that really nice =) Link to comment https://forums.phpfreaks.com/topic/106821-solved-time-elapsed-help/#findComment-548752 Share on other sites More sharing options...
thebadbad Posted May 24, 2008 Share Posted May 24, 2008 To change how it outputs the time, alter getTimeInWords. This should give you what you want (commas instead of "and" and greater detail when dealing with days and shorter periods): <?php function getTimeInWords ($t) { switch (true) { // years and months case $t['y'][0] > 0 && $t['mo'][0] > 0: return "{$t['y'][0]} {$t['y'][1]}, {$t['mo'][0]} {$t['mo'][1]} ago"; case $t['y'][0] > 0 && $t['mo'][0] == 0: return "{$t['y'][0]} {$t['y'][1]}"; // months and days case $t['mo'][0] > 0 && $t['d'][0] > 0: return "{$t['mo'][0]} {$t['mo'][1]}, {$t['d'][0]} {$t['d'][1]} ago"; case $t['mo'][0] > 0 && $t['d'][0] == 0: return "{$t['mo'][0]} {$t['mo'][1]} ago"; // days and hours case $t['d'][0] > 0 && $t['h'][0] > 0: return "{$t['d'][0]} {$t['d'][1]}, {$t['h'][0]} {$t['h'][1]} ago"; case $t['d'][0] > 0 && $t['h'][0] == 0: return "{$t['d'][0]} {$t['d'][1]} ago"; // hours and minutes case $t['h'][0] > 0 && $t['mi'][0] > 0: return "{$t['h'][0]} {$t['h'][1]}, {$t['mi'][0]} {$t['mi'][1]} ago"; case $t['h'][0] > 0 && $t['mi'][0] == 0: return "{$t['h'][0]} {$t['h'][1]} ago"; // minutes and seconds case $t['mi'][0] > 0 && $t['s'][0] > 0: return "{$t['mi'][0]} {$t['mi'][1]}, {$t['s'][0]} {$t['s'][1]} ago"; case $t['mi'][0] > 0 && $t['s'][0] == 0: return "{$t['mi'][0]} {$t['mi'][1]} ago"; // seconds case $t['s'][0] > 10: return 'A moment ago'; default: return 'Just now'; } } ?> Link to comment https://forums.phpfreaks.com/topic/106821-solved-time-elapsed-help/#findComment-548772 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.