Take the 2-minute tour ×

I am trying to convert my already-implemented date format of:

$dateEntered = date('mdY');

To a relative time using this function:

function RelativeTime($dateEntered)

Now, the actual script that runs all of this looks like:


function RelativeTime($dateEntered){
    $difference = time() - $dateEntered;
    $periods = array("sec", "min", "hour", "day", "week",

    if ($difference > 0) { // this was in the past
        $ending = "ago";
    } else { // this was in the future
        $difference = -$difference;
        $ending = "to go";
    for($j = 0; array_key_exists($j,$lengths)&&$difference >= $lengths[$j]; $j++) {
        $difference /= $lengths[$j];
        $difference = round($difference);
        $text = "$difference $periods[$j] $ending";
        return $text;

// Check Service Call Status & Mail if found Unpaid    
$query = "SELECT id, account, status, dateEntered FROM service WHERE status = 'Unpaid'";

while($row = mysql_fetch_row($result)){
    $account = $row[1];
    $status = $row[2];
    $dateEntered = $row[3];

    $timeToSend = RelativeTime(strtotime($dateEntered));

    // mailStatusUpdate($account, $status, $timeToSend);


If I run the script, I get a return of "4 decades ago".

I have my service table with one record in it. The $dateEntered variable returns:


This is shown in the print_r output below. I basically want this to say "x days ago" or whatever the increment is.

Instead of returning the correct information (such as x days ago), it returns:

 4 decades ago

Obviously this is wrong. Here is a print row so you see what I am working with:

Array ( [0] => 26 [1] => Example Client [2] => Unpaid [3] => 01302012 ) 1

(I don't know what that "1" at the end is, though.)

share|improve this question
returning from inside a for loop looks kinda strange to me. –  maiwald Feb 2 '12 at 19:29
add comment

2 Answers

Use example :

echo time_elapsed_string('2013-05-01 00:22:35');
echo time_elapsed_string('2013-05-01 00:22:35', true);

Output :

4 months ago
4 months, 2 weeks, 3 days, 1 hour, 49 minutes, 15 seconds ago

Link to the function.

If you wish to add support for decades, it can be simply implemented like weeks.

share|improve this answer
add comment

Have you verified that your strtotime() usage is getting a correct timestamp from your original $dateEntered value?

Particularly on this line: $timeToSend = RelativeTime(strtotime($dateEntered));


You also might try converting the original date format that you had into something like YYYY-MM-DD for strtotime(), like this:

$dateEntered = substr($dateEntered, 4,4) . '-' . substr($dateEntered, 0, 2) . '-' . substr($dateEntered, 2,2);

This takes the original format: 01302012 and puts it into this format: 2012-01-30. Then, you could use strtotime() like this:

share|improve this answer
My $dateEntered variable gives: 01302012. I need to convert this to the equivalent unix timestamp. If i try to echo $timeToSend I get a "4 decades ago" –  JD Audi Feb 2 '12 at 19:17
I updated my answer to include one possible way to change your 01302012 format into 2012-01-30. Maybe that will work for you? –  summea Feb 2 '12 at 19:31
add comment

Your Answer


