473,396 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to avoid php variable moving before html

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 1293
Dormilich
8,658 Expert Mod 8TB
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
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 Expert Mod 8TB
what does that tell you?
Nov 23 '14 #4
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 Expert Mod 8TB
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
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 Expert Mod 8TB
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
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
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
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
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
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
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
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
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
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
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
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
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,...

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.