Jump to content

Please Help my PHP Dating Function.


Gayner

Recommended Posts

Hi everyone!

 

Well here is my code that displays this:

 

It works wonderful.

 

 

2v8g5yc.jpg

 

The code is:

 

function time_elapsed_string($ptime) {
    $etime = time() - $ptime;
    
    if ($etime < 1) {
        return '0 seconds';
    }
    
    $a = array( 12 * 30 * 24 * 60 * 60  =>  'year',
                30 * 24 * 60 * 60       =>  'month',
                24 * 60 * 60            =>  'day',
                60 * 60                 =>  'hour',
                60                     =>  'minute ago',
                1                       =>  'second'
                );
    
    foreach ($a as $secs => $str) {
        $d = $etime / $secs;
        if ($d >= 1) {
            $r = round($d);
            return $r . ' ' . $str . ($r > 1 ? 's ago' : '');
        }
    }
}

 

But I would like it to say, instead of just 1 Minute ago, make it say "1 Minute 25 Seconds ago"

 

I would want to add seconds into there on the minutes.

 

Can somone revamp this and post again the correct way, Thanks for your patience, ( Sorry for my other topic I was too excited) I'm trying to be nice and professional for u.

Link to comment
Share on other sites

  • Replies 56
  • Created
  • Last Reply

Top Posters In This Topic

you're making this WAY too difficult.  you need to take whatever format $ptime is coming in as, pass it to mktime() to get the unix timestamp and then u can use date() to format it however you want.. DAMN I LOVE PHP!

 

My script is better then ur way why? cause i didn't understand 1 word u said

 

This script works fine  i just need help on adding seconds onto "minutes ego" so it's

 

5Minutes 25seconds ago instead of just 5 Minues ago

Link to comment
Share on other sites

dunno what $ptime stands for but what he said was, if you format $ptime using the php function called mktime(); then it will return a unix formatted time stamp, you can then use the php function named date(); to fromat this timestamp to display the data that you require.

 

search into the functions on php.net.

Link to comment
Share on other sites

dunno what $ptime stands for but what he said was, if you format $ptime using the php function called mktime(); then it will return a unix formatted time stamp, you can then use the php function named date(); to fromat this timestamp to display the data that you require.

 

search into the functions on php.net.

omg.. i know about unix stuff, i use this function around the unix timestamp to spit out "xxx minutes/seconds/ ago"

 

Link to comment
Share on other sites

patronizing people trying to help you doesn't help your cause Gayner.  you need to read up on:

 

http://us2.php.net/manual/en/function.mktime.php

and

http://us2.php.net/manual/en/function.date.php

 

but to be more direct since you're offering no constructive feedback, take this for example:

 

$ptime = "9/20/2009 10:20:59";

$tempArray = explode(" ", $ptime); #break string up by the space char
$dateArray = explode("/", $tempArray[0]); #break date fragment up by "/"
$timeArray = explode(":", $tempArray[1]); #break time fragment by ":"

#convert to unix timestamp
$timestamp = mktime($timeArray[0], $timeArray[1], $timeArray[2], $dateArray[0], $dateArray[1], $dateArray[2]);

#now u can convert to any date/time format you want since u have the unix timestamp
echo date("d m y h:i:s", $timestamp);

 

Edit: and since you can't seem to offer constructive answers to people trying to help you, this will be the last time i help you.  best of luck.

Link to comment
Share on other sites

patronizing people trying to help you doesn't help your cause Gayner.  you need to read up on:

 

http://us2.php.net/manual/en/function.mktime.php

and

http://us2.php.net/manual/en/function.date.php

 

but to be more direct since you're offering no constructive feedback, take this for example:

 

$ptime = "9/20/2009 10:20:59";

$tempArray = explode(" ", $ptime); #break string up by the space char
$dateArray = explode("/", $tempArray[0]); #break date fragment up by "/"
$timeArray = explode(":", $tempArray[1]); #break time fragment by ":"

#convert to unix timestamp
$timestamp = mktime($timeArray[0], $timeArray[1], $timeArray[2], $dateArray[0], $dateArray[1], $dateArray[2]);

#now u can convert to any date/time format you want since u have the unix timestamp
echo date("d m y h:i:s", $timestamp);

 

roflmao were not on the same line  here.

 

 

 

 

Listen up :)

 

I use time() into my row..

 

then when i spit it out i will rap my function around the variable that spit's it out.

 

 

So it would be

 

time_elapsed_string("12353456")

 

or whatever date it is then it will convert that and spit out:

 

0seconds go

 

or w/e

5minutes ago or w/e

 

 

Do u understand now?

 

And this works jsut fine.

 

I just want my script to be enhanced so it will say

 

5Minutes 25seconds ago instead of just "5Minutes ago"

 

Do u understand?

Link to comment
Share on other sites

Neither of you read properly or understood what Gayner wishes to accomplish.

 

I don't think it really makes sense to deal with years and months, as then you have to account for leap years and use slightly different logic.

 

<?php

echo time_elapsed( '2001-01-01 11:34 AM' ) . "\n";
echo time_elapsed( mktime() ) . "\n";
echo time_elapsed( strtotime( "-1 day" ) ) . "\n";
echo time_elapsed( strtotime( "-2 hours -34 minutes" ) ) . "\n";

function time_elapsed( $time ) {
    if( is_string( $time ) ) {
        $time = strtotime( $time );
        if( $time === false ) {
            throw new Exception( "Can not convert time string to timestamp." );
        }
    }
    
    if( is_numeric( $time ) === false ) {
        throw new Exception( "Can't make any sense of the time value." );
    }
    
    $curr_time = mktime();
    $time_diff = $curr_time - $time;
    if( $time_diff < 0 ) {
        return false;
    }
    if( $time_diff === 0 ) {
        return '0 seconds ago.';
    }
    
    static $divisors = null;
    if( $divisors === null ) {
        $divisors = array(
            array( 'day', 24 * 60 * 60 ),
            array( 'hour', 60 * 60 ),
            array( 'minute', 60 ),
            array( 'second', 1 )
        );
    }
    
    $descriptions = array();
    foreach( $divisors as $div ) {
        $val = time_elapsed_helper( $time_diff, $div[1], $div[0] );
        if( $val !== false ) {
            $descriptions[] = $val['description'];
            $time_diff = $val['remainder'];
            if( $time_diff === 0 ) {
                break;
            }
        }
    }
    
    return count( $descriptions ) === 0 ? false : (implode( ' ', $descriptions ) . ' ago');
}

function time_elapsed_helper( $num_seconds, $divisor, $string ) {
    $rv = false;
    
    do {
        $result = (int)($num_seconds / $divisor);
        if( $result === 0 ) break;
        
        if( $result > 1 ) {
            $string .= 's';
        }
        
        $rv = array(
            'description' => $result . ' ' . $string,
            'remainder' => $num_seconds % $divisor
        );
    } while( false );
    return $rv;
}
?>

Link to comment
Share on other sites

Neither of you read properly or understood what Gayner wishes to accomplish.

 

I don't think it really makes sense to deal with years and months, as then you have to account for leap years and use slightly different logic.

 

<?php

echo time_elapsed( '2001-01-01 11:34 AM' ) . "\n";
echo time_elapsed( mktime() ) . "\n";
echo time_elapsed( strtotime( "-1 day" ) ) . "\n";
echo time_elapsed( strtotime( "-2 hours -34 minutes" ) ) . "\n";

function time_elapsed( $time ) {
    if( is_string( $time ) ) {
        $time = strtotime( $time );
        if( $time === false ) {
            throw new Exception( "Can not convert time string to timestamp." );
        }
    }
    
    if( is_numeric( $time ) === false ) {
        throw new Exception( "Can't make any sense of the time value." );
    }
    
    $curr_time = mktime();
    $time_diff = $curr_time - $time;
    if( $time_diff < 0 ) {
        return false;
    }
    if( $time_diff === 0 ) {
        return '0 seconds ago.';
    }
    
    static $divisors = null;
    if( $divisors === null ) {
        $divisors = array(
            array( 'day', 24 * 60 * 60 ),
            array( 'hour', 60 * 60 ),
            array( 'minute', 60 ),
            array( 'second', 1 )
        );
    }
    
    $descriptions = array();
    foreach( $divisors as $div ) {
        $val = time_elapsed_helper( $time_diff, $div[1], $div[0] );
        if( $val !== false ) {
            $descriptions[] = $val['description'];
            $time_diff = $val['remainder'];
            if( $time_diff === 0 ) {
                break;
            }
        }
    }
    
    return count( $descriptions ) === 0 ? false : (implode( ' ', $descriptions ) . ' ago');
}

function time_elapsed_helper( $num_seconds, $divisor, $string ) {
    $rv = false;
    
    do {
        $result = (int)($num_seconds / $divisor);
        if( $result === 0 ) break;
        
        if( $result > 1 ) {
            $string .= 's';
        }
        
        $rv = array(
            'description' => $result . ' ' . $string,
            'remainder' => $num_seconds % $divisor
        );
    } while( false );
    return $rv;
}
?>

 

Roopurt, thanks but when i wrap time_elapsed('1258446691') around my variables it just shows up nothing,

 

but it will do it if the date is like

 

time_elapsed( '2001-01-01 11:34 AM' ) . "\n";

 

or something

 

hmm ?

Link to comment
Share on other sites

basically do what i said at the end of your function, make a while loop you input $ptime into the variable $etime so once you have analysed it the first time then you will get the minuites for example, minus how many minuites you returned from $etime and set it and then analyse it again, the main while loop would say while($etime > 0){}

Link to comment
Share on other sites

<?php

echo time_elapsed( '2001-01-01 11:34 AM' ) . "\n";
echo time_elapsed( mktime() ) . "\n";
echo time_elapsed( strtotime( "-1 day" ) ) . "\n";
echo time_elapsed( strtotime( "-2 hours -34 minutes" ) ) . "\n";

function time_elapsed( $time ) {
    if( is_string( $time ) === true && is_numeric( $time ) === false ) {
        $time = strtotime( $time );
        if( $time === false ) {
            throw new Exception( "Can not convert time string to timestamp." );
        }
    }
    
    if( is_numeric( $time ) === false ) {
        throw new Exception( "Can't make any sense of the time value." );
    }
    
    $curr_time = mktime();
    $time_diff = $curr_time - $time;
    if( $time_diff < 0 ) {
        return false;
    }
    if( $time_diff === 0 ) {
        return '0 seconds ago.';
    }
    
    static $divisors = null;
    if( $divisors === null ) {
        $divisors = array(
            array( 'day', 24 * 60 * 60 ),
            array( 'hour', 60 * 60 ),
            array( 'minute', 60 ),
            array( 'second', 1 )
        );
    }
    
    $descriptions = array();
    foreach( $divisors as $div ) {
        $val = time_elapsed_helper( $time_diff, $div[1], $div[0] );
        if( $val !== false ) {
            $descriptions[] = $val['description'];
            $time_diff = $val['remainder'];
            if( $time_diff === 0 ) {
                break;
            }
        }
    }
    
    return count( $descriptions ) === 0 ? false : (implode( ' ', $descriptions ) . ' ago');
}

function time_elapsed_helper( $num_seconds, $divisor, $string ) {
    $rv = false;
    
    do {
        $result = (int)($num_seconds / $divisor);
        if( $result === 0 ) break;
        
        if( $result > 1 ) {
            $string .= 's';
        }
        
        $rv = array(
            'description' => $result . ' ' . $string,
            'remainder' => $num_seconds % $divisor
        );
    } while( false );
    return $rv;
}
?>

Link to comment
Share on other sites

Here is what you should do, I commented every line of the code, even the ones that you wrote so that you know what is going on.

 

<?php
    function time_elapsed_string($ptime)
    {
        /* Elapsed time */
        $etime = time() - $ptime;

        /* Less than a second so return that */
        if($etime < 1)
        {
            return '0 seconds ago.';
        }

        /* Crate an array of all of the time values */
        $a = array(
            (12 * (30 * (24 * (60 * 60))))    =>  'year',
            (30 * (24 * (60 * 60)))            =>  'month',
            (24 * (60 * 60))                =>  'day',
            (60 * 60)                        =>  'hour',
            60                                =>  'minute',
            1                                =>  'second'
        );

        /* Create a blank output string ready for analysis */
        $outputString = '';

        /* For each item in the time values array */
        foreach($a as $secs => $str)
        {

            /* difference is the time elapsed devided by the seconds */
            $d = $etime / $secs;

            /* If the difference is larger than one */
            if($d >= 1)
            {
                /* Round the difference */
                $r = round($d);

                if($r > 1)
                {
                    /* Add the value to the output string with s on the end */
                    $outputString .= $r . ' ' . $str . 's ';
                } else {
                    /* Add the value to the output string */
                    $outputString .= $r . ' ' . $str . ' ';
                }

                /* Minus the time that we just analysed from the elapsed time so that when it gets analysed */
                /* for the next variable in the array it does not return something like 61 second(s) */
                $etime -= ($r * $secs);
            }
        }

        /* Add the ago onto the end */
        $outputString .= 'ago.';

        /* Return the output string. */
        return $outputString;
    }

    /* Simple test of the time elapsed function. */
    echo(time_elapsed_string(time() - ((60 * 60) + 1)));
    echo('<br>');
    echo(time_elapsed_string(time() - ((60 * 60) + 2)));
?>

 

I took particualr interest in this as i tought that it would be a nice feature to add to my site, so thankyou and you're welcome.

 

Craig.

 

EDIT: Added a little fix for the s's on the end of the values.

 

You can check it out here: http://gaming-network.ath.cx/time.php

 

If my site is up that is :) .

Link to comment
Share on other sites

<?php
echo time_elapsed_string( strtotime( '-1 year -12 months' ) ) . "\n";
echo time_elapsed_string( strtotime( '-4 year -6 months' ) ) . "\n";
echo time_elapsed_string( strtotime( '-12 months' ) ) . "\n";
echo time_elapsed_string( strtotime( '-11 months' ) ) . "\n";

    function time_elapsed_string($ptime)
    {
        /* Elapsed time */
        $etime = time() - $ptime;

        /* Less than a second so return that */
        if($etime < 1)
        {
            return '0 seconds ago';
        }

        /* Crate an array of all of the time values */
        /* You could modify this function later on when it checks what the values are which could, */
        /* add the (s) on automatically if needed as 1 would be 1 hour but 2 would be 2 hours */
        $a = array(
            (12 * (30 * (24 * (60 * 60))))    =>  'year(s)',
            (30 * (24 * (60 * 60)))            =>  'month(s)',
            (24 * (60 * 60))                =>  'day(s)',
            (60 * 60)                        =>  'hour(s)',
            60                                =>  'minute(s)',
            1                                =>  'second(s)'
        );

        /* Create a blank output string ready for analysis */
        $outputString = '';

        /* For each item in the time values array */
        foreach($a as $secs => $str)
        {

            /* difference is the time elapsed devided by the seconds */
            $d = $etime / $secs;

            /* If the difference is larger than one */
            if($d >= 1)
            {
                /* Round the difference */
                $r = round($d);

                /* Add the value to the output string */
                $outputString .= $r . ' ' . $str . ' ';

                /* Minus the time that we just analysed from the elapsed time so that when it gets analysed */
                /* for the next variable in the array it does not return something like 61 second(s) */
                $etime -= ($r * $secs);
            }
        }

        /* Add the ago onto the end */
        $outputString .= 'ago.';

        /* Return the output string. */
        return $outputString;
    }
exit();
?>

 

yields:

 

2 year(s) 11 day(s) ago.
5 year(s) ago.
1 year(s) 5 day(s) ago.
11 month(s) 5 day(s) ago.

Link to comment
Share on other sites

Well don’t use negative times then, it is to check the times between the current time and a $ptime previous time to tell the difference.

 

If you want to scrutinise then add the few lines of code that check the timestamp to see if it is a negatively addressed one and then act upon that instead of just saying it is wrong.

 

For the purpose of the OP's needs and my own personal needs then this works fine for now thanks.

Link to comment
Share on other sites

Negative times?  There is no such thing and I have no idea what you're talking about.

 

You provided a function that:

1) Takes a parameter: A time in the past

2) Returns: The elapsed time between now and the parameter, as a text string

 

So how come when I give your function a timestamp that is exactly 2 years ago your function returns 2 year(s) 11 day(s) ago?

Link to comment
Share on other sites

I believe this is more accurate, but it requires more testing than I have time to do right now:

 

<?php
echo 'Current: ' . date( 'Y-m-d H:i:s' ) . "\n";
echo time_elapsed( strtotime( '-1 year -12 months' ) ) . "\n";
echo time_elapsed( strtotime( '-4 year -6 months' ) ) . "\n";
echo time_elapsed( strtotime( '-12 months' ) ) . "\n";
echo time_elapsed( strtotime( '-11 months' ) ) . "\n";
echo time_elapsed( strtotime( '2009-10-18' ) ) . "\n";
echo time_elapsed( strtotime( '2009-10-04' ) ) . "\n";
echo time_elapsed( strtotime( '2009-10-29' ) ) . "\n";
echo time_elapsed( strtotime( '2008-02-18' ) ) . "\n";
echo time_elapsed( strtotime( '2008-02-04' ) ) . "\n";
echo time_elapsed( strtotime( '2008-02-28 09:21:00' ) ) . "\n";
echo time_elapsed( strtotime( '2008-02-28 09:35:00' ) ) . "\n";
echo time_elapsed( strtotime( '2008-02-28 08:00:45' ) ) . "\n";
echo time_elapsed( strtotime( '2008-02-28 19:15:23' ) ) . "\n";
echo time_elapsed( mktime() ) . "\n";
echo time_elapsed( strtotime( "-1 year -1 day" ) ) . "\n";
echo time_elapsed( strtotime( "-2 hours -34 minutes -20 seconds" ) ) . "\n";
echo time_elapsed( strtotime( "-2 hours -98 minutes" ) ) . "\n";

function time_elapsed( $time ) {
    if( is_string( $time ) && is_numeric( $time ) === false ) {
        $time = strtotime( $time );
        if( $time === false ) {
            throw new Exception( "Can not convert time string to timestamp." );
        }
    }
    
    if( is_numeric( $time ) === false ) {
        throw new Exception( "Can't make any sense of the time value." );
    }
    
    $curr = mktime();
    
    if( $time == $curr ) {
        return 'right now';
    }
    
    $tY = date( 'Y', $time );
    $tm = date( 'm', $time );
    $td = date( 'd', $time );
    $tH = date( 'H', $time );
    $ti = date( 'i', $time );
    $ts = date( 's', $time );
    
    $cY = date( 'Y', $curr );
    $cm = date( 'm', $curr );
    $cd = date( 'd', $curr );
    $cH = date( 'H', $curr );
    $ci = date( 'i', $curr );
    $cs = date( 's', $curr );
    
    $YD = $cY - $tY;
    $mD = $cm - $tm;
    $dD = $cd - $td;
    $HD = $cH - $tH;
    $iD = $ci - $ti;
    $sD = $cs - $ts;
    
    $values = array();
    
    if( $YD >= 2 || ($YD == 1 && $tm <= $cm) ) {
        $values[] = array( 'year', $YD );
    }
    
    $mod = $dD < 0 ? -1 : 0;
    if( $mD >= 1 ) {
        $values[] = array( 'month', $mD + $mod );
    }else if( $mD < 0 ) {
        $values[] = array( 'month', (12 - $tm) + $cm + $mod );
    }
    
    $mod = $HD < 0 ? -1 : 0;
    if( $dD >= 1 ) {
        $values[] = array( 'day', $dD + $mod );
    }else if( $dD < 0 ) {
        $values[] = array( 'day', (date( 't', $time) - $td) + $cd + $mod );
    }
    
    $mod = $iD < 0 ? -1 : 0;
    if( $HD >= 1 ) {
        $values[] = array( 'hour', $HD + $mod );
    }else if( $HD < 0 ) {
        $values[] = array( 'hour', (24 - $tH) + $cH + $mod );
    }
    
    $mod = $sD < 0 ? -1 : 0;
    if( $iD >= 1 ) {
        $values[] = array( 'minute', $iD + $mod );
    }else if( $iD < 0 ) {
        $values[] = array( 'minute', (60 - $ti) + $ci + $mod );
    }
    
    if( $sD >= 1 ) {
        $values[] = array( 'second', $sD );
    }
    
    foreach( $values as $k => $v ) {
        if( $v[1] > 1 ) {
            $v[0] .= 's';
        }
        $values[$k] = $v[1] . ' ' . $v[0];
        if( $v[1] == 0 ) {
            unset( $values[$k] );
        }
    }
    
    return implode( ' ', $values ) . ' ago';
}
?>

 

yields:

 

Current: 2009-11-18 10:33:16
2 years ago
4 years 6 months ago
1 year ago
11 months ago
1 month 10 hours 33 minutes 16 seconds ago
1 month 14 days 10 hours 33 minutes 16 seconds ago
20 days 10 hours 33 minutes 16 seconds ago
1 year 9 months 10 hours 33 minutes 16 seconds ago
1 year 9 months 14 days 10 hours 33 minutes 16 seconds ago
1 year 8 months 19 days 1 hour 12 minutes 16 seconds ago
1 year 8 months 19 days 58 minutes 16 seconds ago
1 year 8 months 19 days 2 hours 32 minutes ago
1 year 8 months 18 days 15 hours 17 minutes ago
right now
1 year 1 day ago
2 hours 34 minutes ago
3 hours 38 minutes ago

Link to comment
Share on other sites

Fixed something with the seconds calculation:

 

<?php
function time_elapsed( $time ) {
    if( is_string( $time ) && is_numeric( $time ) === false ) {
        $time = strtotime( $time );
        if( $time === false ) {
            throw new Exception( "Can not convert time string to timestamp." );
        }
    }
    
    if( is_numeric( $time ) === false ) {
        throw new Exception( "Can't make any sense of the time value." );
    }
    
    $curr = mktime();
    
    if( $time == $curr ) {
        return 'right now';
    }
    
    $tY = date( 'Y', $time );
    $tm = date( 'm', $time );
    $td = date( 'd', $time );
    $tH = date( 'H', $time );
    $ti = date( 'i', $time );
    $ts = date( 's', $time );
    
    $cY = date( 'Y', $curr );
    $cm = date( 'm', $curr );
    $cd = date( 'd', $curr );
    $cH = date( 'H', $curr );
    $ci = date( 'i', $curr );
    $cs = date( 's', $curr );
    
    $YD = $cY - $tY;
    $mD = $cm - $tm;
    $dD = $cd - $td;
    $HD = $cH - $tH;
    $iD = $ci - $ti;
    $sD = $cs - $ts;

    $values = array();
    
    if( $YD >= 2 || ($YD == 1 && $tm <= $cm) ) {
        $values[] = array( 'year', $YD );
    }
    
    $mod = $dD < 0 ? -1 : 0;
    if( $mD >= 1 ) {
        $values[] = array( 'month', $mD + $mod );
    }else if( $mD < 0 ) {
        $values[] = array( 'month', (12 - $tm) + $cm + $mod );
    }
    
    $mod = $HD < 0 ? -1 : 0;
    if( $dD >= 1 ) {
        $values[] = array( 'day', $dD + $mod );
    }else if( $dD < 0 ) {
        $values[] = array( 'day', (date( 't', $time) - $td) + $cd + $mod );
    }
    
    $mod = $iD < 0 ? -1 : 0;
    if( $HD >= 1 ) {
        $values[] = array( 'hour', $HD + $mod );
    }else if( $HD < 0 ) {
        $values[] = array( 'hour', (24 - $tH) + $cH + $mod );
    }
    
    $mod = $sD < 0 ? -1 : 0;
    if( $iD >= 1 ) {
        $values[] = array( 'minute', $iD + $mod );
    }else if( $iD < 0 ) {
        $values[] = array( 'minute', (60 - $ti) + $ci + $mod );
    }
    
    if( $sD >= 1 ) {
        $values[] = array( 'second', $sD );
    }else if( $sD < 0 ) {
        $values[] = array( 'second', 60 + $sD );
    }
    
    foreach( $values as $k => $v ) {
        if( $v[1] > 1 ) {
            $v[0] .= 's';
        }
        $values[$k] = $v[1] . ' ' . $v[0];
        if( $v[1] == 0 ) {
            unset( $values[$k] );
        }
    }
    
    return implode( ' ', $values ) . ' ago';
}
?>

Link to comment
Share on other sites

Ahh OK I get you now, sorry I thought that you were saying that I was wrong completley with my methodology.

 

simply change:

 

$etime -= ($d * $secs);

 

to

 

$etime -= date($r * $secs);

 

New code:

 

<?php
    function time_elapsed_string($ptime)
    {
        /* Elapsed time */
        $etime = time() - $ptime;

        /* Less than a second so return that */
        if($etime < 1)
        {
            return '0 seconds ago.';
        }

        /* Crate an array of all of the time values */
        $a = array(
            (12 * (30 * (24 * (60 * 60))))    =>  'year',
            (30 * (24 * (60 * 60)))            =>  'month',
            (24 * (60 * 60))                =>  'day',
            (60 * 60)                        =>  'hour',
            60                                =>  'minute',
            1                                =>  'second'
        );

        /* Create a blank output string ready for analysis */
        $outputString = '';

        /* For each item in the time values array */
        foreach($a as $secs => $str)
        {

            /* difference is the time elapsed devided by the seconds */
            $d = $etime / $secs;

            /* If the difference is larger than one */
            if($d >= 1)
            {
                /* Round the difference */
                $r = round($d);

                if($r > 1)
                {
                    /* Add the value to the output string with s on the end */
                    $outputString .= $r . ' ' . $str . 's ';
                } else {
                    /* Add the value to the output string */
                    $outputString .= $r . ' ' . $str . ' ';
                }

                /* Minus the time that we just analysed from the elapsed time so that when it gets analysed */
                /* for the next variable in the array it does not return something like 61 second(s) */
                $etime -= date($r * $secs);
            }
        }

        /* Add the ago onto the end */
        $outputString .= 'ago.';

        /* Return the output string. */
        return $outputString;
    }
?>

<h1>Elapsed Time Function</h1>

<h2>Solution for:</h2>
<p>
    <a href="http://www.phpfreaks.com/forums/index.php/topic,277182.msg1312173.html#msg1312173">Thread</a>
</p>

<!-- Simple tests of the time elapsed function. -->
<h2>A coupple of simple tests:</h2>
<p>
    <?php echo(time_elapsed_string(time() - ((60 * 60) + 1))); ?>
</p>

<p>
    <?php echo(time_elapsed_string(time() - ((60 * 60) + 2))); ?>
</p>

<p>
    <?php echo(time_elapsed_string(strtotime('-1 year -12 months'))); ?>
</p>

<p>
    <?php echo(time_elapsed_string(strtotime('-1 year -2 months -3 minutes -21 seconds'))); ?>
</p>

 

EDIT: I wondered why $r * $secs wasnt working lol :P.

 

I suppose you could make it a tad more efficient aswell by getting rid of some variables and moving the if statment from the top down to the bottom with an else statment. Aswell as that I now see that I can add the values to the array by using the date(); function.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.