Modified post page for assignment files.
[Assignment-Trapper.git] / time.php
blobcc1466f3598eeab815cb3040bada1134eea0a04d
1 <?php
3 function humanTiming ($time)
5 $time = time() - $time; // to get the time since that moment
7 $tokens = array (
8 31536000 => 'year',
9 2592000 => 'month',
10 604800 => 'week',
11 86400 => 'day',
12 3600 => 'hour',
13 60 => 'minute',
14 1 => 'second'
17 foreach ($tokens as $unit => $text) {
18 if ($time < $unit) continue;
19 $numberOfUnits = floor($time / $unit);
20 return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
24 function absHumanTiming($time)
26 $out = '';
27 $now_date = date("U", time());
28 $time = strtotime($time);
30 if($now_date < $time) {
31 $time = ($time - $now_date); // number of seconds into the future
32 $time = $now_date - $time; // subtract difference from current time
33 $out .= humanTiming($time)." left";
34 } else {
35 $out .= humanTiming($time)." ago";
37 return $out;
40 function tab2space($text, $spaces = 4)
42 // Explode the text into an array of single lines
43 $lines = explode("\n", $text);
45 // Loop through each line
46 foreach ($lines as $line)
48 // Break out of the loop when there are no more tabs to replace
49 while (false !== $tab_pos = strpos($line, "\t"))
51 // Break the string apart, insert spaces then concatenate
52 $start = substr($line, 0, $tab_pos);
53 $tab = str_repeat(' ', $spaces - $tab_pos % $spaces);
54 $end = substr($line, $tab_pos + 1);
55 $line = $start . $tab . $end;
58 $result[] = $line;
61 return implode("\n", $result);