PHP function calculate and display date time interval or time difference

Posted by admin
This simple PHP function calculate and display's date time interval/difference of a two given value which is date time start/before and the current date time. This is useful when you want to display how old the record or articles in textual format.

<?
function calculate_time_ago($datefrom, $using_timestamps=FALSE){

if (!$using_timestamps){
$datefrom = strtotime($datefrom, 0);
}// Convert time to timestamp

$difference = (time() - $datefrom);// Difference in seconds

if($difference < 60){ //1 second
$rtnval = $difference." sec ago";
} elseif($difference < 3600){ //1 hour
$rtnval = round(($difference / 60), 0)." min ago";
} elseif($difference < 86400){ //1 day
$rtnval = round(($difference / 3600), 0)." hrs ago";
} elseif($difference < 604800){ //7 days
$rtnval = round(($difference / 86400), 0)." days ago";
} elseif($difference < 2419200){ //4 weeks
$rtnval = round(($difference / 604800), 0)." weeks ago";
} elseif($difference < 31537800){ //365 1/2 days in 1 year
$rtnval = round(($difference / 2419200), 0)." months ago";
} elseif($difference > 31537800){ //365 1/2 days in 1 year
$rtnval = round(($difference / 31537800), 0)." years ago";
} else{
$rtnval = "NA";
}

return $rtnval;
}
?>
Back to List | Comments (0) | January 14, 2010 at 01:02 am

0 Responses

No Records Yet.

Leave a Reply

Name (required)

Email (required) will not be publish

Website (optional) ex: http://www.domain.com

Notify me of followup comments via e-mail. (optional)

Your Message (required)

Enter Security Code (required)
captcha  

Latest News (2)

iWebProvider.com No 1 at Yahoo search engine for the keyword -filipino web developer.

Posted on October 02, 2009

On Sept 6, 2009 iWebProvider.com is No. 1 at Yahoo and Google for keyword "filipino web developer".

continue reading

Launching of the updated iWebProvider.com

Posted on October 02, 2009

Now as of July 7, 2009 the launching of iWebProvider.com with its improve feature and the used of its very own CMS. It now has features like Blogs, comments on News articles and the upcoming user registration.

continue reading

Latest Comments (21)

gravatar.jpg Hi,

Its interesting for the SEO friendly URL creatin we had followed it and created using mod re-write with htaccess file you can see real PHP page converted into .html hahaha.. see here http://www.addonsolutions.com/design-a-web-2.0-website.html

Thanks
Mayur

- by: Mayur Shah

Wise Words

"It's not about how many you have learned but how much you have utilized and mastered what you have learned."

- by: Mark Lester
Get subscribers