0

I am using the following code to calculate the time difference between a post and the current time:

function format_interval(DateInterval $interval) {
            $result = "";
            if ($interval->y) { $result .= $interval->format("%y y "); }
            if ($interval->m) { $result .= $interval->format("%m m "); }
            if ($interval->d) { $result .= $interval->format("%d d "); }
            if ($interval->h) { $result .= $interval->format("%h h "); }
            if ($interval->i) { $result .= $interval->format("%i m "); }
            if ($interval->s) { $result .= $interval->format("%s s "); }

            return $result;
        }

        $first_date = new DateTime($row['updatecomment']);
        $second_date = new DateTime("now");

        $difference = $first_date->diff($second_date);

        $post_date = format_interval($difference);

I would then like to enter $post_date into the following echo string:

if ($row['comment'] != null) {
            echo '<div class="rowComment">' . '<div class="postComment" id="postcomment">' . $row['comment'] . '</div>' . '<div class="row3_a">' . "Posted: '.$post_date.' . Ago" . '</div>' . '</div>';

However, what I current get in return is:

Posted: '..' . Ago

I've searched the forums to get to this point. I feel I am very close, but I am just not getting the time difference output.

1 Answer 1

1

You have a few mismatched quotes.

if ($row['comment'] != null)
            echo '<div class="rowComment">' . '<div class="postComment" id="postcomment">' . $row['comment'] . '</div>' . '<div class="row3_a">' . "Posted: $post_date Ago" . '</div></div>';
Sign up to request clarification or add additional context in comments.

5 Comments

that returns the following: Posted: Ago
That means that $post_date is an empty string.
hmm. when i remove the $post_date = and echo the format_interval($difference); it returns the time difference. Can i enter format_interval($difference); into the echo string above instead of the variable?
That's odd, make sure that the variable is spelled correctly. Otherwise I guess you could do that, just pass format_interval($difference) instead of $post_date...
echo '<div class="rowComment">' . '<div class="postComment" id="postcomment">' . $row['comment'] . '</div>' . '<div class="row3_a">'."Posted: ".format_interval($difference)." Ago" . '</div>' . '</div>';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.