How to avoid php variable moving before html

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bala Kumaran
    New Member
    • Jan 2013
    • 30

    How to avoid php variable moving before html

    Hello,
    The following script is the "times ago" function

    Code:
    <?php
    function time_stamp($session_time) 
    { 
    $time_difference = time() - $session_time ; 
    
    $seconds = $time_difference ; 
    $minutes = round($time_difference / 60 );
    $hours = round($time_difference / 3600 ); 
    $days = round($time_difference / 86400 ); 
    $weeks = round($time_difference / 604800 ); 
    $months = round($time_difference / 2419200 ); 
    $years = round($time_difference / 29030400 ); 
    // Seconds
    if($seconds <= 60)
    {
    echo "$seconds seconds ago"; 
    }
    //Minutes
    else if($minutes <=60)
    {
    
       if($minutes==1)
      {
       echo "one minute ago"; 
       }
       else
       {
        echo "$minutes minutes ago"; 
       }
    
    }
    //Hours
    else if($hours <=24)
    {
    
       if($hours==1)
      {
       echo "one hour ago";
      }
      else
      {
       echo "$hours hours ago";
      }
    
    }
    //Days
    else if($days <= 7)
    {
    
      if($days==1)
      {
       echo "one day ago";
      }
      else
      {
       echo "$days days ago";
       }
    
    }
    //Weeks
    else if($weeks <= 4)
    {
    
       if($weeks==1)
      {
       echo "one week ago";
       }
      else
      {
       echo "$weeks weeks ago";
      }
    
    }
    //Months
    else if($months <=12)
    {
    
       if($months==1)
      {
       echo "one month ago";
       }
      else
      {
       echo "$months months ago";
       }
    
    }
    //Years
    else
    {
    
       if($years==1)
       {
        echo "one year ago";
       }
       else
      {
        echo "$years years ago";
       }
    
    }
    
    } 
    
    
    $session_time = "2012-11-22 22:54:50";
    $times_ago = time_stamp(strtotime($session_time));
    //$session_time=time();
    echo "<h1>You got a friend request from kathir ".$times_ago."</h1><br />";
    ?>
    The script is working properly. but, The php variable is not showing between the html tags. It displays only before the tags.

    From the above code I am getting the output as

    6 minutes ago You got a friend request from kathir.


    I want to get the output like as

    You got a friend request from kathir 6 minutes ago.

    How should I want to modify the script?
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    It displays only before the tags.
    because you print it there.

    check this simple test: var_dump(time_s tamp(strtotime( $session_time)) );

    Comment

    • Bala Kumaran
      New Member
      • Jan 2013
      • 30

      #3
      Thx for ur reply. The same problem accuring again and it displays a new "null" value after that. :(

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        what does that tell you?

        Comment

        • Bala Kumaran
          New Member
          • Jan 2013
          • 30

          #5
          I am getting the output look like this

          6 minutes ago null You got a friend request from kathir.

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            your function as it is posted prints the output when you call it. it doesn’t save that in any return value which you didn’t define anyways.

            Comment

            • Bala Kumaran
              New Member
              • Jan 2013
              • 30

              #7
              Okay. I understood. If I want to display a times ago function between tags like that. How sholud I want to create the code? Could you help me?

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                either place the function where you want the result printed or make the function return the values instead of printing them.

                Comment

                Working...