How can I output the return from a function

lezz

I have a Timeago function but I don't know how I can return the Timeago into a variable that I can display on my web page. I'm very new to PHP and I have grabbed a Timeago function off the Net, but I don't know how to pass the result into a variable that i can echo out on my web page.

The code is below...

<?php

$dateadded = $row['dateadded'];

$timeadded=$row['timeadded'];

I would like to pass into the function the variables above from date('y-m-d'); and time('h-i-s');, but I'm not sure how to go about it, and more importantly, how do i echo this out on my page as a variable?

return "$difference $periods[$j] 'ago' ";

thank you for any help ...

function ago($time)
{
   $periods = array("second", "minute", "hour", "day", "week", "month", 
"year", "decade");
   $lengths = array("60","60","24","7","4.35","12","10");

   $now = time();

   $difference     = $now - $time;
   $tense         = "ago";

   for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
       $difference /= $lengths[$j];
    }

      $difference = round($difference);

   if($difference != 1) {
       $periods[$j].= "s";
   }

   return "$difference $periods[$j] 'ago' ";
}
?>

I have managed to get the "days ago" working with a little code, but I would also like to show hours if the days are below 1 day. Any idea how I can adapt the below code to show in hours if the daysago is 0?

$start = strtotime($dateadded);

$end = strtotime(date('y-m-d'));

$daysago = ceil(abs($end - $start) / 86400);

I tried this but it did not work:

$hours = $daysago / ( 60 * 60 );

$mins = $hours /(60*60);

//if ($hours < 1) {$hours=$mins;}
//if ($daysago < 1) {$daysago = $hours; }

and I was displaying the result in a title tag like below:

<div id='contentimagegallery' class='contentimagegallery' style='z-index:1000;' title='{$daysago}'>

but I would like to be able to show the result in hours also.

Christian

The thing with PHP is that you can use data gathered on the server side to build your Views that are going to be displayed on the client side. So, from what I see in your code there, you are gathering the data that the user is writing in an input on your webpage right? Having the data gathering and the function to calculate the 'timeago', like you're naming it, is half of the work! Let's get to the view crafting. There are two ways on doing this:

  • Make views like classes.
  • Make views like scripts.

Views as classes:

Imagine that you have your 'timeago' algorithm in your PHP script, and you also have your HTML view, probably with a .html extension. Let's create a view that instantiates with a parameter, which will be the result of your timeago operation:

<?php

class My_View{

  var $timeago;

  function __construct($timeago){
    $this->timeago = $timeago;
    $this->render();
  }

  function render(){
    ?>

    <html>
      <head>
        <title>Timeago Example</title>
      </head>
      <body>
        <h1>Your timeago is: <?php echo $this->timeago; ?></h1>
      </body>
    </html>

    <?php
  }

}

Just a pretty simple HTML that's created with a parameter (as the function __constructor() says). Look how I open the <?php tag when it's PHP code, and write pure HTML outside that tag, after the closing PHP tag ?>. That class is going to output an <h1> with the value of the class attribute $timeago, and as it's a class attribute, you have to refer to it with $this->timeago. Your code will end up with something like this:

<?php

//Include your view
//Notice the .php extension!!
include __DIR__.'/../Views/My_View.php';

// Your stuff here

$timeago = ago($time); //this is your function
new My_View($timeago);
exit();

And that's it! Let's now look how to do it without classes.

Views as scripts

Now, instead of creating a class, just create a simple script that's suposed to use a $timeago variable. We will call it 'my_view.php':

<html>
      <head>
        <title>Timeago Example</title>
      </head>
      <body>
        <h1>Your timeago is: <?php echo $timeago; ?></h1>
      </body>
</html>

See how we only have a PHP tag with an echo of $timeago? Let's see how we could use your code for printing that View and I'll then explain to you why does it work:

// Do not include your View here

// Your stuff here

$timeago = ago($time); //this is your function

include __DIR__.'/../Views/my_view.php';
exit();

So the question here is... If I have made a script called my_view.php that prints a $timeago variable, why does it print it? That script does not know about that variable's existance. That's because of the include keyword. Every time you include or include_once a file, it's like you're copying and pasting the file's content directly in the middle of your script, and guess what... That file will know about all the variables created before in that same script.

Nowadays, people use templates for building the dynamic HTML Views, but if you're going to continue with pure PHP, I would highly recommend using Classes, as they are easier to maintain and just looking at the __construct() method, you'll know all the variables being used by that particular View.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I return a JavaScript string from a WebAssembly function

How can I return a variable from a $.getJSON function

How can I return two values from a function in Python?

How can I return value from function onResponse of Volley?

How can I return value from function onResponse of Retrofit?

How can I return a character array from a function in C?

How can I return a JoinHandle from a function?

How can I return null or bool from a function

How can I return a value from a function that uses json response?

How can I yield notifications and return a result from a function? (Python)

How can I return a grpc::ClientContext from a function?

How can I return a string literal from a Postgres function?

How can I return a React.Component class from a function?

How can I return an array in javascript from a function?

How can I exclude a output from a function that randomizes the output of an array?

How can I return a matplotlib figure from a function?

How can I return key value pair from a function

How can I return a function?

How can I return a value from a parent function in Node?

how can i return the count from a function

How can I print a return value from a function in Java?

How can I return a generic type from a member function in Rust

How can I return a string from an async function?

How can I return an integer value from this function Multi

How can I run a function and return output as image src javascript

How can I return a div from forEach inside map function?

How can I return a value from an async/awaited function?

How can I return a specific object from a generic function?

How can I return an array from a function?