Current location: Hot Scripts Forums » Programming Languages » PHP » Help with timedifference function


Help with timedifference function

Reply
  #1 (permalink)  
Old 03-08-10, 05:53 PM
sarahmx sarahmx is offline
Newbie Coder
 
Join Date: Jan 2010
Posts: 27
Thanks: 5
Thanked 0 Times in 0 Posts
Question Help with timedifference function

Hi its me again....

I'm trying to adapt the timediffrence function by JCBones here in a new program of mine..the code previously shows only hours and not days..right now i also want to show day if it after 24 hour..

like 1 day ago...2 days ago etc


right now my new code is working except it still show hours if if excedeed 24 hours....example

http://img695.imageshack.us/img695/2149/41633294.jpg

PHP Code:

function timeDifference($start,$end,$return='days') {
    
$difference max($end$start) - min($end,$start);
    
$time NULL;
    
//calculate time difference.
    
switch($return) {
        case 
'days':
             
$days floor($difference/86400);
                
$difference $difference 86400;
                    
$time['days'] = $days;
        case 
'hours':
            
$hours floor($difference/3600);
                
$difference $difference 3600;
                    
$time['hours'] = $hours;
        case 
'minutes':
            
$minutes floor($difference/60);
                
$difference $difference 60;
                    
$time['minutes'] = $minutes;
        case 
'seconds':
            
$seconds $difference;
                
$time['seconds'] = $seconds;
    }
    
    
$output = array();
       if(
is_array($time)) {
        
$showSec true;

if(isset(
$time['days']) && $time['days'] > 0) {
                        
$output[] = $time['days'] . ' Days';
                        
$showSec false;
                    }


                    if(isset(
$time['hours']) && $time['hours'] > 0) {
                        
$output[] = $time['hours'] . ' Hours';
                        
$showSec false;
                    }
                    
                    if(isset(
$time['minutes']) && $time['minutes'] > 0) {
                        
$output[] = $time['minutes'] . ' Minutes';
                        
$showSec false;
                    }
                    
                    if(isset(
$time['seconds']) && $showSec == true) {
                        return 
$time['seconds'] . ' Seconds';
                    }
                
        
            return 
implode(', ',$output);
        }
}  


$now time();
$hours 24;
$time = ( intval$hours ) > ) ? time() - ( intval$hours ) * 3600 ) : ( time() - 24*3600 ); 

PHP Code:

while($row mysql_fetch_array($result))
{

$id=$row['id']; 
$mydate=$row['date'];
$showdate$mydate formatTimestamp($mydate) : '';
$adate=$row[date];
$showdate2 ' ' timeDifference($adate,$now,'hours') . ' ago ';

echo 
'<tr class="', ++$int 'even' 'odd''">'
echo 
"<td>"
echo 
$countmesej++;
echo 
"</td>";
echo 
"<td>" $row['mesej_title'] . "</td>";
echo 
"<td>test: $showdate | $showdate2  </td></tr>";


this must be simple..but i can't figure this out...help a noob
p/s: sorry for my english

Last edited by sarahmx; 03-08-10 at 06:02 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 03-08-10, 07:36 PM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
Try this:
PHP Code:

while($row mysql_fetch_array($result))
{

$id=$row['id']; 
$mydate=$row['date'];
$showdate$mydate formatTimestamp($mydate) : '';
$adate=$row[date];
$showdate2 ' ' timeDifference($adate,$now,'days') . ' ago ';

echo 
'<tr class="', ++$int 'even' 'odd''">'
echo 
"<td>"
echo 
$countmesej++;
echo 
"</td>";
echo 
"<td>" $row['mesej_title'] . "</td>";
echo 
"<td>test: $showdate | $showdate2  </td></tr>";


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 03-08-10, 11:37 PM
sarahmx sarahmx is offline
Newbie Coder
 
Join Date: Jan 2010
Posts: 27
Thanks: 5
Thanked 0 Times in 0 Posts
thanks Jcbones

i'm trying to do this 3 thing


1. 2 days ago instead of 2 Days, 1 Hours, 59 Minutes ago
2. 1 month ago if exceed 30/31 days
3. 1 year ago if exceed 1 year


any help ?

Last edited by sarahmx; 03-08-10 at 11:40 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 03-09-10, 01:37 AM
sarahmx sarahmx is offline
Newbie Coder
 
Join Date: Jan 2010
Posts: 27
Thanks: 5
Thanked 0 Times in 0 Posts
ok i found this...i want to do some like this..using the jcbones functions

PHP Code:

function _ago($tm,$rcs 0) {
    
$cur_tm time(); $dif $cur_tm-$tm;
    
$pds = array('second','minute','hour','day','week','month','year','decade');
    
$lngh = array(1,60,3600,86400,604800,2630880,31570560,315705600);
    for(
$v sizeof($lngh)-1; ($v >= 0)&&(($no $dif/$lngh[$v])<=1); $v--); if($v 0$v 0$_tm $cur_tm-($dif%$lngh[$v]);
   
    
$no floor($no); if($no <> 1$pds[$v] .='s'$x=sprintf("%d %s ",$no,$pds[$v]);
    if((
$rcs == 1)&&($v >= 1)&&(($cur_tm-$_tm) > 0)) $x .= time_ago($_tm);
    return 
$x;

second','minute','hour','day','week','month','year ','decade'

Last edited by sarahmx; 03-09-10 at 01:42 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 03-09-10, 07:59 PM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
Try this:

PHP Code:

function timeDifference($start,$end,$return='decade') {
    
$difference max($end$start) - min($end,$start);
    
$time NULL;
    
//calculate time difference.
    
switch($return) {
        case 
'decade':
            
$decade floor($difference/315705600);
                
$difference $difference 315705600;
                    
$time['decade'] = $decade;
        case 
'year':
            
$year floor($difference/31570560);
                
$difference $difference 31570560;
                    
$time['year'] = $year;
        case 
'month':
            
$month floor($difference/2630880);
                
$difference $difference 2630880;
                    
$time['month'] = $month;
        case 
'week':
            
$week floor($difference/604800);
                
$difference $difference 604800;
                    
$time['week'] = $week;
        case 
'days':
             
$days floor($difference/86400);
                
$difference $difference 86400;
                    
$time['day'] = $days;
        case 
'hours':
            
$hours floor($difference/3600);
                
$difference $difference 3600;
                    
$time['hour'] = $hours;
        case 
'minutes':
            
$minutes floor($difference/60);
                
$difference $difference 60;
                    
$time['minute'] = $minutes;
        case 
'seconds':
            
$seconds $difference;
                
$time['second'] = $seconds;
    }   
    
       if(
is_array($time)) {
            foreach(
$time as $key => $value) {
                if(
$value 0) {
                    if(
$value 1) {
                        
$key $key 's';
                    }
                    return 
$value ' ' $key ' ago.';
                }
            }
        }
    return 
false;

PHP Code:

while($row mysql_fetch_array($result))
{

$id=$row['id']; 
$mydate=$row['date'];
$showdate$mydate formatTimestamp($mydate) : '';
$adate=$row[date];
$showdate2 ' ' timeDifference($adate,time());

echo 
'<tr class="', ++$int 'even' 'odd''">'
echo 
"<td>"
echo 
$countmesej++;
echo 
"</td>";
echo 
"<td>" $row['mesej_title'] . "</td>";
echo 
"<td>test: $showdate | $showdate2  </td></tr>";


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
The Following User Says Thank You to Jcbones For This Useful Post:
sarahmx (03-09-10)
  #6 (permalink)  
Old 03-09-10, 08:29 PM
sarahmx sarahmx is offline
Newbie Coder
 
Join Date: Jan 2010
Posts: 27
Thanks: 5
Thanked 0 Times in 0 Posts
thank you jcbones....
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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 On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[2005] StartPosition for CommonDialogs? tim8w Windows .NET Programming 10 01-08-09 04:39 AM
ASP upload prob minority ASP 1 06-27-05 09:35 AM
PHP Error Fairnie PHP 8 06-26-04 08:15 AM
Disable form fields to be submitted RickyRod JavaScript 2 05-24-04 11:15 AM
Help trim code down TheLaughingBandit JavaScript 0 09-02-03 10:50 AM


All times are GMT -5. The time now is 06:52 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.