difference in time

This is a discussion on difference in time within the PHP General forums, part of the PHP Programming Forums category; hi to all! on one eZine site, I have to show when the article is posted but as difference from ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-10-2008
Lamp Lists
 
Posts: n/a
Default difference in time

hi to all!
on one eZine site, I have to show when the article is posted but as difference from NOW. like "posted 32 minutes ago", or "posted 5 days ago".

is there already sucha php/mysql function?

thanks.

-ll


---------------------------------
Never miss a thing. Make Yahoo your homepage.
Reply With Quote
  #2 (permalink)  
Old 03-11-2008
Aschwin Wesselius
 
Posts: n/a
Default Re: [PHP] difference in time

Lamp Lists wrote:
> hi to all!
> on one eZine site, I have to show when the article is posted but as difference from NOW. like "posted 32 minutes ago", or "posted 5 days ago".
>
> is there already sucha php/mysql function?
>
> thanks.


Hi,

I don't know about existing functions but you can use this:

function time_to_units($time) {

$years = floor($time / 60 / 60 / 24/ 365);
$time -= $years * 60 * 60 * 24 * 365;
$months = floor($time / 60 / 60 / 24 / 30);
$time -= $months * 60 * 60 * 24 * 30;
$weeks = floor($time / 60 / 60 / 24 / 7);
$time -= $weeks * 60 * 60 * 24 * 7;
$days = floor($time / 60 / 60 / 24);
$time -= $days * 60 * 60 * 24;
$hours = floor($time / 60 / 60);
$time -= $hours * 60 * 60;
$minutes = floor($time / 60);
$time -= $minutes * 60;
$seconds = $time;
$amount = 0;
$unit = '';

if ($years > 0) {

$amount = $years;
$unit = ' year';

if ($years > 1) {

$unit.= 's ';
}
}
elseif ($months > 0) {

$amount = $months;
$unit = ' month';

if ($months > 1) {

$unit.= 's ';
}
}
elseif ($weeks > 0) {

$amount = $weeks;
$unit = ' week';

if ($weeks > 1) {

$unit.= 's ';
}
}
elseif ($days > 0) {

$amount = $days;
$unit = ' day';

if ($days > 1) {

$unit.= 's ';
}
}
elseif ($hours > 0) {

$amount = $hours;
$unit = ' hour';

if ($hours > 1) {

$unit.= 's ';
}
}
elseif ($minutes > 0) {

$amount = $minutes;
$unit = ' minute';

if ($minutes > 1) {

$unit.= 's ';
}
}
elseif ($seconds > 0) {

$amount = $seconds;
$unit = ' second';

if ($seconds > 1) {

$unit.= 's ';
}
}

return $amount.$unit;
}

$posted = ; // some timestamp in the past
$now = time(); // as current as possible
$diff = ($now - $posted);

echo "posted ".time_to_units($diff)." ago";

I hope this helps ;-)
--

Aschwin Wesselius

/'What you would like to be done to you, do that to the other....'/

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 06:14 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0