473,499 Members | 1,672 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to avoid php variable moving before html

30 New Member
Hello,
The following script is the "times ago" function

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. function time_stamp($session_time) 
  3. $time_difference = time() - $session_time ; 
  4.  
  5. $seconds = $time_difference ; 
  6. $minutes = round($time_difference / 60 );
  7. $hours = round($time_difference / 3600 ); 
  8. $days = round($time_difference / 86400 ); 
  9. $weeks = round($time_difference / 604800 ); 
  10. $months = round($time_difference / 2419200 ); 
  11. $years = round($time_difference / 29030400 ); 
  12. // Seconds
  13. if($seconds <= 60)
  14. {
  15. echo "$seconds seconds ago"; 
  16. }
  17. //Minutes
  18. else if($minutes <=60)
  19. {
  20.  
  21.    if($minutes==1)
  22.   {
  23.    echo "one minute ago"; 
  24.    }
  25.    else
  26.    {
  27.     echo "$minutes minutes ago"; 
  28.    }
  29.  
  30. }
  31. //Hours
  32. else if($hours <=24)
  33. {
  34.  
  35.    if($hours==1)
  36.   {
  37.    echo "one hour ago";
  38.   }
  39.   else
  40.   {
  41.    echo "$hours hours ago";
  42.   }
  43.  
  44. }
  45. //Days
  46. else if($days <= 7)
  47. {
  48.  
  49.   if($days==1)
  50.   {
  51.    echo "one day ago";
  52.   }
  53.   else
  54.   {
  55.    echo "$days days ago";
  56.    }
  57.  
  58. }
  59. //Weeks
  60. else if($weeks <= 4)
  61. {
  62.  
  63.    if($weeks==1)
  64.   {
  65.    echo "one week ago";
  66.    }
  67.   else
  68.   {
  69.    echo "$weeks weeks ago";
  70.   }
  71.  
  72. }
  73. //Months
  74. else if($months <=12)
  75. {
  76.  
  77.    if($months==1)
  78.   {
  79.    echo "one month ago";
  80.    }
  81.   else
  82.   {
  83.    echo "$months months ago";
  84.    }
  85.  
  86. }
  87. //Years
  88. else
  89. {
  90.  
  91.    if($years==1)
  92.    {
  93.     echo "one year ago";
  94.    }
  95.    else
  96.   {
  97.     echo "$years years ago";
  98.    }
  99.  
  100. }
  101.  
  102.  
  103.  
  104. $session_time = "2012-11-22 22:54:50";
  105. $times_ago = time_stamp(strtotime($session_time));
  106. //$session_time=time();
  107. echo "<h1>You got a friend request from kathir ".$times_ago."</h1><br />";
  108. ?>
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?
Nov 22 '14 #1
7 1295
Dormilich
8,658 Recognized Expert Moderator Expert
It displays only before the tags.
because you print it there.

check this simple test: var_dump(time_stamp(strtotime($session_time)));
Nov 23 '14 #2
Bala Kumaran
30 New Member
Thx for ur reply. The same problem accuring again and it displays a new "null" value after that. :(
Nov 23 '14 #3
Dormilich
8,658 Recognized Expert Moderator Expert
what does that tell you?
Nov 23 '14 #4
Bala Kumaran
30 New Member
I am getting the output look like this

6 minutes ago null You got a friend request from kathir.
Nov 23 '14 #5
Dormilich
8,658 Recognized Expert Moderator Expert
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.
Nov 24 '14 #6
Bala Kumaran
30 New Member
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?
Nov 25 '14 #7
Dormilich
8,658 Recognized Expert Moderator Expert
either place the function where you want the result printed or make the function return the values instead of printing them.
Nov 25 '14 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

7
3430
by: olga | last post by:
Hi, On my site, i want to pass a javascript variable to php. I know that this needs to done in a link or in a post. I want to know if there is a way i can do it with an html link. I should...
2
2326
by: simon | last post by:
hello, what i'm looking to do is store the path of the app on a the server for reuse in the site. my thoughts so far are... -make a key in the web.config file -retrieve the value in globals.asax...
1
24144
by: amit | last post by:
Hello Group, Does anybody know how I can have a global variable in an HTML file? for instance, I have a fuction (called aFunction() here) and during a mousedown or up event the function is going...
1
3113
by: JohnIdol | last post by:
Hi all, a quick one here. I am moving elements on the page clientside and I am going through strange issues. This one works: <html> <head> <title>Javascript Text</title>
8
5165
by: roop1 | last post by:
I am using a perl script to produce a six element array. When I click a string in the first element, I am linked to a html table (all.html) and the matching string is displayed in the first row of...
6
3150
by: roop1 | last post by:
Hello gits, I found this snippet that you offered to another person and I was wondering if it wouldn't work for me as well: <script type="text/javascript"> function...
1
1238
jenkinsloveschicken
by: jenkinsloveschicken | last post by:
I am needing help passing a GET variable to a receiving php page. This html page does not contain an forms for submission(POST/GET methods). First I need to parse the URL string using...
4
2302
by: Pakku | last post by:
On this site http://www.onlamp.com/pub/a/php/2001/05/03/php_foundations.html I discovered that I could embed php variables in html using this notation The variable $var has a value of:...
4
13248
by: jessy | last post by:
i need to know if its possible to write the value of a Javascript variable inside HTML tag ..and here's the line : var x=document.getElementById('div1').value; formdiv.innerHTML =...
1
2634
by: kkshansid | last post by:
i want this page open only for registered users as much as i know session variable doesnt work in html page kindly help me to do this thanx in advance studentsearch.htm <html> <body> <script...
0
7131
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7174
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7388
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5470
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4600
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3099
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3091
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.