-2

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:

<?php

function RelativeTime($dateEntered){
    $difference = time() - $dateEntered;
    $periods = array("sec", "min", "hour", "day", "week",
        "month", "years", "decade");
    $lengths = array("60","60","24","7","4.35","12","10");

    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);
        if($difference != 1) $periods[$j].= "s";
        $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'";
$result = mysql_query($query) or die(mysql_error());

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:

01302012

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.)

BenMorel
  • 34,448
  • 50
  • 182
  • 322
JD Audi
  • 1,067
  • 6
  • 18
  • 37

2 Answers2

0

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.

Community
  • 1
  • 1
Glavić
  • 42,781
  • 13
  • 77
  • 107
0

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));

Edit:

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:

strtotime($dateEntered);
summea
  • 7,390
  • 4
  • 32
  • 48
  • 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 02 '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 02 '12 at 19:31