Page 1 of 1

[Solved] Date Function, Help

Posted: Sat Apr 30, 2005 7:35 pm
by anthony88guy
I made a small function to display the difference between two dates.

Code: Select all

<?php
function displaytime($postedtime, $time){
	$diff = $time-$postedtime;
		if($diff < 60){
			return date("s", $diff) . " seconds ago";
		}elseif($diff >= 60 && $diff < 3600){
			return date("i", $diff) . " minutes ago";
		}elseif($diff >= 3600 && $diff < 86400){
			return date("H", $diff) . " hours ago";
		}elseif($diff >= 86400){
			return date("d", $diff) . " days ago";
		}else{
			return "Older Than 1 Month";
		}
}
?>
The seconds and minutes work, but the hours dont. I am getting 17hours when it should be about 1.3hours. Whats going wrong?

Posted: Sat Apr 30, 2005 9:25 pm
by Skara
well, first let's fix the function itself, then find the error you mean.

Code: Select all

<?php
function displaytime($postedtime, $time) {
    $diff = $time - $postedtime;
    if ($diff < 60) {
        return date("s", $diff) . " seconds ago";
    }
    elseif ($diff < 3600) {
        return date("i", $diff) . " minutes ago";
    }
    elseif ($diff < 86400) {
        return date("H", $diff) . " hours ago";
    }
    elseif ($diff < ????????) {
        return date("d", $diff) . " days ago";
    }
    else {
        return "Older Than 1 Month";
    }
}
?>
If it skips the one elseif statement, then obviously it evaluated to false--you don't need to check it again.
On the last, it would always say how many days old it was. You had 'if $diff is greater than one day.' You need to check if it's smaller than a month. As I said before, if it's parsing said statement, it already knows it's greater than a day.

also, you should space your code out a bit more--kinda hard to read.
if you like shorter code, then you don't really need the brackets:

Code: Select all

<?php
function displaytime($postedtime, $time) {
    $diff = $time - $postedtime;
    if     ($diff < 60)       return date("s", $diff) . " seconds ago";
    elseif ($diff < 3600)     return date("i", $diff) . " minutes ago";
    elseif ($diff < 86400)    return date("H", $diff) . " hours ago";
    elseif ($diff < ????????) return date("d", $diff) . " days ago";
    else                      return "Older Than 1 Month";
}
?>

Posted: Sun May 01, 2005 10:50 am
by anthony88guy
Thanks I like how you space out the code. I will try that from now on, and it is easier to read.

Code: Select all

<?php
function displaytime($postedtime, $time) {
    $diff = $time - $postedtime;
    if ($diff < 60) {
        return date("s", $diff) . " seconds ago";
    }
    elseif ($diff < 3600) {
        return date("i", $diff) . " minutes ago";
    }
    elseif ($diff < 86400) {
        return date("H", $diff) . " hours ago";
    }
    elseif ($diff < 2678400) {
        return date("d", $diff) . " days ago";
    }
    else {
        return "Older Than 1 Month";
    }
}
?>
My problem is this: The posted time I have is: 1114913490, and current time is this: 1114962325. Now when you subtract the two you get: 48835, when you divide it by 60 to get minutes you get: 813.916667. So divide again by 60 and you get hours: 13.5652778. Now when I use the function in my script it returns: 05 hours ago. What’s going wrong?

Posted: Sun May 01, 2005 1:35 pm
by Ambush Commander
What you can do is have some data consistency checks: have the script in the function echo out what they've got at that moment to you, and then check it against what you calculated.

Code: Select all

&lt;?php
function displaytime($postedtime, $time) {
    echo 'Time message was posted is '.$postedtime;
    echo 'Time you are comparing it against is '.$time;
    $diff = $time - $postedtime;
    echo 'Difference is '.$diff.'&lt;br /&gt;';
    if ($diff &lt; 60) {
        return date(&quote;s&quote;, $diff) . &quote; seconds ago&quote;;
    }
    elseif ($diff &lt; 3600) {
        return date(&quote;i&quote;, $diff) . &quote; minutes ago&quote;;
    }
    elseif ($diff &lt; 86400) {
        echo 'Hours is being parsed&lt;br /&gt;';
        echo date(&quote;i&quote;, $diff).'&lt;br /&gt;';
        return date(&quote;H&quote;, $diff) . &quote; hours ago&quote;;
    }
    elseif ($diff &lt; 2678400) {
        return date(&quote;d&quote;, $diff) . &quote; days ago&quote;;
    }
    else {
        return &quote;Older Than 1 Month&quote;;
    }
}
?&gt;