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