Sunday 15 May 2011

mysql - PHP time elapsed, remove "and" before seconds -



mysql - PHP time elapsed, remove "and" before seconds -

so, i've got working code converts mysql date unix timestamp , subtracts current date() show "time elapsed since x"-like timer. (the part takes date database missing since it's in script)

<?php function time_elapsed($secs){ $bit = array( ' year' => $secs / 31556926 % 12, ' week' => $secs / 604800 % 52, ' day' => $secs / 86400 % 7, ' hour' => $secs / 3600 % 24, ' minute' => $secs / 60 % 60, ' second' => $secs % 60 ); foreach($bit $k => $v){ if($v > 1)$ret[] = $v . $k . 's'; if($v == 1)$ret[] = $v . $k; } array_splice($ret, count($ret)-1, 0, 'and'); $ret[] = 'ago.'; homecoming join(' ', $ret); } $nowtime = time() + 10; //add 10s avoid error $oldtime = strtotime($mysqltime2); $time_elapsed = time_elapsed($nowtime-$oldtime)."\n"; echo wordwrap($time_elapsed,35,"<br />\n"); //split long line ?>

i managed prepare error reporting missing array or if script executed @ same time of mysql date adding 10 seconds current timestamp.

another issue have script shows "and x seconds" if there aren't minutes/hours/days/etc before it. e.g. "and x seconds" "y minutes and x seconds" "z hours y minutes and x seconds" "n days z hours y minutes and x seconds"

i want remove "and" before seconds if there's no minutes/hours/etc before it.

any tip on how prepare this?

you splice when needed:

if( count($ret) > 1 ) { array_splice($ret, count($ret)-1, 0, 'and'); }

php mysql unix unix-timestamp elapsedtime

No comments:

Post a Comment