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.
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:
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.
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.
Comments