/ Published in: PHP
Timespan like twitter "ago" time.
Expand |
Embed | Plain Text
function timespan($time1, $time2 = NULL, $output = 'years,months,weeks,days,hours,minutes,seconds') { // Array with the output formats // Invalid output return FALSE; // Make the output values into keys // Default values // Calculate timespan (seconds) // All values found using Google Calculator. // Years and months do not match the formula exactly, due to leap years. // Years ago, 60 * 60 * 24 * 365 // Months ago, 60 * 60 * 24 * 30 // Weeks ago, 60 * 60 * 24 * 7 // Days ago, 60 * 60 * 24 // Hours ago, 60 * 60 // Minutes ago, 60 // Seconds ago, 1 // Remove the variables that cannot be accessed // Deny access to these variables // Return the difference foreach ($output as $key) { { // Add requested key to the output $difference[$key] = $$key; } } // Invalid output formats string return FALSE; // If only one output format was asked, don't put it in an array // Return array return $difference; }
You need to login to post a comment.
