Posted on: 01/10/05 09:55am
By: jetshack
Posted on: 01/10/05 12:23pm
By: machinari
<?php /* Works out the time since the entry post, takes a an argument in unix time (seconds) */ function time_since($original) { // array of time period chunks $chunks = array( array(60 * 60 * 24 * 365 , 'year'), array(60 * 60 * 24 * 30 , 'month'), array(60 * 60 * 24 * 7, 'week'), array(60 * 60 * 24 , 'day'), array(60 * 60 , 'hour'), array(60 , 'minute'), ); $today = time(); /* Current unix time */ $since = $today - $original; // $j saves performing the count function each time around the loop for ($i = 0, $j = count($chunks); $i < $j; $i++) { $seconds = $chunks[$i][0]; $name = $chunks[$i][1]; // finding the biggest chunk (if the chunk fits, break) if (($count = floor($since / $seconds)) != 0) { // DEBUG print "<!-- It's $name -->n"; break; } } $print = ($count == 1) ? '1 '.$name : "$count {$name}s"; if ($i + 1 < $j) { // now getting the second item $seconds2 = $chunks[$i + 1][0]; $name2 = $chunks[$i + 1][1]; // add second item if it's greater than 0 if (($count2 = floor(($since - ($seconds * $count)) / $seconds2)) != 0) { $print .= ($count2 == 1) ? ', 1 '.$name2 : ", $count2 {$name2}s"; } } return $print; } ?>found another one at this page.
<? function elapsed($year, $month, $day, $hour, $minute) { // No copyright, use and fiddle with as you see fit, just don't steal credit for it, // as it is based on the work of myself and several other people. // Call the function with the command elapsed(yyyy,mm,dd,hh,mm), for example: // elapsed(1974,5,14,06,10) will produce the elapsed time since my birth date. // This script will automatically omit parts that return a zero value, so that 1 year, // 3 months, 0 days, 0 hours and 0 minutes will not show the days, hours and minutes, // as that would be quite pointless and ugly. $todayMonth = date(n); $todayDay = date(j); $todayHour = date(G); $todayMinute = date(i); $monthday = date(nj); $link = $month.$day; $datepoint = $link; if ($monthday < $datepoint) $years = date(Y) - $year - 1; else $years = date(Y) - $year; if ($years < 1) $yearWord = ''; elseif ($years == 1) $yearWord = 'year,'; elseif ($years > 1) $yearWord = 'years,'; if ($todayMonth > $month) { if ($todayDay < $day) $months = $todayMonth - $month -1; else $months = $todayMonth - $month; } else { $between = $month - $todayMonth; $plus = 12 - $month; if ($todayDay < $day) $months = $month + $plus - $between -1; else $months = $month + $plus - $between; } if ($months == 1) $monthWord = 'month,'; elseif ($months > 1) $monthWord = 'months,'; if ($todayDay > $day) { if ($todayHour < $hour) $days = $todayDay - $day -1; else $days = $todayDay - $day; } else { $between = $day - $todayDay; $daysToEndOfMonth = date(t) - $day; if (date(t) == 28) $plus = $daysToEndOfMonth; if (date(t) == 29) $plus = $daysToEndOfMonth; if (date(t) == 30) $plus = $daysToEndOfMonth; if (date(t) == 31) $plus = $daysToEndOfMonth; if ($todayHour < $hour) $days = $day + $plus - $between - 1; else $days = $day + $plus - $between; } if ($days == 1) $dayWord = 'day,'; else $dayWord = 'days,'; if ($todayHour > $hour) { if ($todayMinute < $minute) $hours = $todayHour - $hour - 1; else $hours = $todayHour - $hour; } else { $between = $hour - $todayHour; $plus = 24 - $hour; if ($todayMinute < $minute) $hours = $hour + $plus - $between - 1; else $hours = $hour + $plus - $between; } if ($hours == 1) $hourWord = 'hour,'; if ($hours > 1) $hourWord = 'hours,'; if ($todayMinute > $minute) { $minutes = $todayMinute - $minute; } else { $between = $minute - $todayMinute; $plus = 60 - $minute; $minutes = $minute + $plus - $between; } if ($minutes == 60) $minutes = 0; if ($minutes == 1) $minuteWord = 'minute'; elseif ($minutes > 1) $minuteWord = 'minutes'; if ($years == 0) { $years = ''; $yearWord = ''; } if ($months == 0) { $months = ''; $monthWord = ''; } if ($days == 0) { $days = ''; $dayWord = ''; } if ($hours == 0) { $hours = ''; $hourWord = ''; } if ($minutes == 0) { $minutes = ''; $minuteWord = ''; } echo $years.' '.$yearWord.' '.$months.' '.$monthWord.' '.$days.' '.$dayWord.' '.$hours.' '.$hourWord.' '.$minutes.' '.$minuteWord.''; } ?>lots of these around the net. hope that helps.
Posted on: 01/10/05 03:41pm
By: jetshack
/** * timesince PHP Block function * * Copenhagen free */ function phpblock_timesince() { $chunks = array( array(60 * 60 * 24 * 365 , 'year'), array(60 * 60 * 24 * 30 , 'month'), array(60 * 60 * 24 * 7, 'week'), array(60 * 60 * 24 , 'day'), array(60 * 60 , 'hour'), array(60 , 'minute'), ); $today = time(); /* Current unix time */ $since = $today - $original; // $j saves performing the count function each time around the loop for ($i = 0, $j = count($chunks); $i < $j; $i++) { $seconds = $chunks[$i][0]; $name = $chunks[$i][1]; // finding the biggest chunk (if the chunk fits, break) if (($count = floor($since / $seconds)) != 0) { // DEBUG print "<!-- It's $name -->n"; break; } } $print = ($count == 1) ? '1 '.$name : "$count {$name}s"; if ($i + 1 < $j) { // now getting the second item $seconds2 = $chunks[$i + 1][0]; $name2 = $chunks[$i + 1][1]; // add second item if it's greater than 0 if (($count2 = floor(($since - ($seconds * $count)) / $seconds2)) != 0) { $print .= ($count2 == 1) ? ', 1 '.$name2 : ", $count2 {$name2}s"; } } return $print; }
Posted on: 01/10/05 05:15pm
By: machinari
Posted on: 01/10/05 11:18pm
By: jetshack
} } return $print; }
} } print("Jon's been Copenhagen free for "); return $print;
Posted on: 01/11/05 12:37am
By: drshakagee
} } $print .= 'Jon's been Copenhagen free for '; return $print;
Posted on: 01/11/05 09:14am
By: jetshack
Posted on: 01/11/05 11:52am
By: machinari
function phpblock_timesince() { $original = '483627987';//some date in unix time. (seconds)
Posted on: 01/11/05 12:13pm
By: machinari
function phpblock_timesince() { $original = mktime(0,0,0,5,17,2002);// mktime(hour,min,sec,month,day,year)
Posted on: 01/11/05 02:33pm
By: jetshack
/** * timesince PHP Block function * * Copenhagen free */ function phpblock_timesince() { $chunks = array( array(60 * 60 * 24 * 365 , 'year'), array(60 * 60 * 24 * 30 , 'month'), array(60 * 60 * 24 * 7, 'week'), array(60 * 60 * 24 , 'day'), array(60 * 60 , 'hour'), array(60 , 'minute'), ); $original = mktime(1,1,1,1,2,2005); $today = time(); /* Current unix time */ $since = $today - $original; // $j saves performing the count function each time around the loop for ($i = 0, $j = count($chunks); $i < $j; $i++) { $seconds = $chunks[$i][0]; $name = $chunks[$i][1]; // finding the biggest chunk (if the chunk fits, break) if (($count = floor($since / $seconds)) != 0) { // DEBUG print "<!-- It's $name -->n"; break; } } if ($i + 1 < $j) { // now getting the second item $seconds2 = $chunks[$i + 1][0]; $name2 = $chunks[$i + 1][1]; $print = 'Jon has been Copenhagen free for '; $print .= ($count == 1) ? '1 '.$name : "$count {$name}s"; // add second item if it's greater than 0 if (($count2 = floor(($since - ($seconds * $count)) / $seconds2)) != 0) { $print .= ($count2 == 1) ? ', 1 '.$name2 : ", $count2 {$name2}s"; } } return $print; }
$print = 'Jon has been Copenhagen free for '; $print .= ($count == 1) ? '1 '.$name : "$count {$name}s";