PHP - Calculate How Long Since Last Visit
Hi
I'm trying to figure out how to calculate how long since a datevariable I have stored is from whenever the page is loaded.. I currently have a stored datevariable in the format: YYYY-MM-DD H:m:s ... So basically what I wish to accomplish is figuring out whenever i load the page which has stored this variable, how much time has passed. I want it to show up as for example : '10 minutes ago'.. '1 hour 25minutes ago' if it's the same day.. if its more than 1 day old it's enough to show only the number of days.....'1week 3 days ago' etc etc Similar Tutorialshi guys, I'm trying to use a very basic PHP script and i think I've got everything right but I'm not getting any records inserted into my table. here is my script: $dbHost = "ip address"; $dbName= "rptDatabase"; $dbUsername = "username"; $dbPassword = "password"; $ip = $_SERVER['REMOTE_ADDR']; $page = $_SERVER['PHP_SELF']; $referrer = $_SERVER['HTTP_REFERER']; $date = date("m/d/y"); $time = date("h:i:a"); $db = mysql_connect($dbHost, $dbUsername, $dbPassword) or trigger_error(mysql_error(),E_USER_ERROR); mysql_select_db($dbName, $db); $sql = "INSERT INTO tblTraffic (ip, page, referrer, date, time) VALUES ($ip, $page, $referrer, $date, $time)"; $result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR); mysql_close($db); can someone tell me what I'm doing wrong? thanks. Edited October 13, 2019 by ajetrumpetGood day: Im trying to add a number every time a person visits the page. here is the code Im using. Any help will be appreciated. Code: [Select] <?php $aid = $_GET['aid']; $connection = mysql_connect("localhost", "username", "password"); mysql_select_db("articles", $connection); $query="SELECT visits FROM articles_description WHERE id=$aid"; $result=mysql_query($query); $num=mysql_numrows($result); <?php while ($j < $num) { $visits = $num[0] + 1; } $query = "UPDATE articles_description SET visits = $visits WHERE id = $aid"; if(mysql_query($sql_query)) { echo "Record updated successfully"; $j++; } mysql_close(); ?> This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=322423.0 I just had a question for you guys. I am building a forum script and I'm currently stumped on the "show unread posts since last visit" feature. Not sure as to how to code it. Should I be using a mysql database to keep track of them or use cookies some how? Thanks in advanced! Hi All, I am trying to call a javascript pop-up window via a link by echoing out the html via php but keep getting parse errors: Parse error: syntax error, unexpected ')', expecting ',' or ';' Here is the code in question: Code: [Select] echo "<td class=\"today\"> <a href=\"javascript:statusWindow('status.php?month=".$month."&day=".$day."&year=$year');\">"$day_num</a> </td>\n"; And here is the javascript function: Code: [Select] <script type="text/javascript"> function statusWindow(url){ status_popupWin = window.open(url, 'status', 'resizable=yes, scrollbars=yes, toolbar=no,width=400,height=400'); status_popupWin.opener = self; } </script> Any help is appreciated. Thanks, kaiman Hey Folks This is a major long shot but is their a way that i can use php to upload one mp3 file and one doc file, then get the php script to place the mp3 file in one folder but also copy the mp3 file and make it into a zip file and place it into another folder as well as also make another copy of the mp3 file pair it with the doc file and make it into another zip file in yet another folder mp3->---------------------------->--mp3----------------->----mp3 into zip-------->--mp3 + doc into zip &---- > --Uploaded via php----->--placed in folder a--->----placed in folder b--->--placed in folder c doc-->---------------------------->--copied--------------->----mp3 copied--------->-- But thier is a catch the host i am using didn't activate ZipArchive() function so need a diferent way to make the zip file. Any Ideas people? because i have been pulling my hair out at this one please help Phil I would like to apply some addition functionality to a form which add news records into the database. Basicaly what i want is similar to what this forum does with the [ code ] [ /code ] but what i want for the user to be able to add [ quote ] [ /quote ] and when the post is viewed then that specific area of the acticle body will be formated diffrently. I hope this makes sence. I try to get how long my message was sent with this... $Now = strtotime ("now"); $dateEnvoi = $fetchDiscussion['dateEnvoi']; //my date from my DB $timeEnvoi = mktime($dateEnvoi); $TempsEnvoi = $dateNow - $dateEnvoi; and it try to get how long it was post ... same thing as on facebook... like the 26 minutes ago from FB how can i do it ? is it possible to get an ajax call to execute a php script that will continue running even if the user leaves my site? i have used cron jobs and tokens to do in past but this can result in 1 min delay. If I break up this long line of code will it still work correctly? Original... Code: [Select] define('WEB_ROOT', ENVIRONMENT === 'dev' ? 'http://localhost/example.org/' : 'http://example.org/'); Desired... Code: [Select] define('WEB_ROOT', ENVIRONMENT === 'dev' ? 'http://localhost/example.org/' : 'http://example.org/'); Or maybe... Code: [Select] define('WEB_ROOT', ENVIRONMENT === 'dev' ? 'http://localhost/example.org/' : 'http://example.org/'); Debbie I've written a script thats going to take a really long time to execute... Whats the best way to do this as far as the server not timing out? My script is calculating some statistics based on other tables in the database and then storing them in another table. The results are going to be hundreds of thousands or maybe even millions of rows. I have the following code on a page Code: [Select] <?PHP session_start();?> <HTML> <HEAD> <META http-equiv="Content-Type" content="text/html; charset=windows-1252"> <TITLE>Running Test Cricket</TITLE> </HEAD> <BODY> <?PHP echo "Time at start ".$_SERVER['REQUEST_TIME']; #This is our hourly updates for teams if it is due - it is set off by the variable 'teamUpdateTime' echo "<BR>1"; $updateTime=time(); #Get the current time and put it into the variable $teams = mysql_query("SELECT * FROM teams WHERE teamUpdateTime<'$updateTime'"); //Go through all the teams while($team = mysql_fetch_array($teams)) { echo "<BR>UPDATING - hourly - ".$team['name']; #Show the team we are updating include("../updates/hourly_update_team.php"); #Update all the teams that are due to be updated } echo "<BR>2"; mysql_close($sqldb); //Close the database echo "<BR>Time at end ".$_SERVER['REQUEST_TIME']; ?> </BODY></HTML>While the code is crude it is not for the public to see - it is for updating teams in an online game I am creating. What my problem is that the REQUEST_TIME at the start and end of the code are always outputted the same; the code is there to see how long the page takes to do. I sit there and count how long the page takes to activate (load, whatever the terminology is) and it can take up to 40 seconds - but the REQUEST_TIME tells me it takes 0 seconds so obviously something is wrong. My question is simple - how do I get this code to output the REQUEST_TIME when the code starts and when it finished, thus showing me how long it took for the page to load. I wish to do this to try and speed it up. James Guys, why the following mysql stored procedure taking much more time comparing with firebird one?
1. MySQL stored procedure
CREATE DEFINER=`lxc`@`::1` PROCEDURE `insertData`(IN it INT) BEGIN declare i int Default 0; declare str char(4); declare p_hash int; myloop: loop set p_hash = round(rand() * 999); set str='book'; insert into test.users (name,password) VALUES (concat(str,'_',i+1),PASSWORD(p_hash)); set i = i+1; if (i = it) then leave myloop; end if; end loop myloop; endObject info: Table: users Columns: id int(10) UN PK AI name varchar(45) password char(41)Time: mysql> call insertData(10000); Query OK, 1 row affected (9 min 38.22 sec)2. Firebird Stored Procedure SET TERM ^ ; CREATE PROCEDURE INSERDATA ( IT integer ) --iteration number AS DECLARE VARIABLE i integer; BEGIN i = 0; while ( i < it ) do begin INSERT INTO PRODUCT( CODE,NAME,PASS) VALUES(:i, 'jazz_' || :i, fb_hash(round(rand() * 999))); i = i + 1; end END^ SET TERM ; ^ COMMIT;Object Info: Table: product columns CODE integer, NAME varchar(45), PASS CHAR(41)Time: ( for 10 000 records it takes less than 1 sec for execution) Executing statement... Statement executed (elapsed time: 0.063s). 31464 fetches, 11084 marks, 0 reads, 0 writes. 10000 inserts, 0 updates, 0 deletes, 5 index, 0 seq. Delta memory: 24264 bytes. PRODUCT: 10000 inserts. 0 rows affected directly. Total execution time: 0.151s Script execution finished.Do you see something wrong in mysql stored procedure code? jazz. Edited by jazzman1, 31 July 2014 - 03:21 PM. I'm working on a site that provides database results. To get to the final report, I'm going through a few pages where the user selects specific options. This builds up arrays that need to be passed from one page to another. As a result the final page has a pretty lengthy variable list. Since I want to provide a link to certain customers where they don't have to go through all the option select stuff, I'm using the GET method on the final page. Due to all the arrays I need to pass, I'm hitting hyperlink address character limitations, and I fear I will eventually hit browser URL length limits. Is there any way to provide links to customers without using the GET method so I could keep the URL length down? Hello , I've this code its skip the weekend ,and I've a table for special events has field datetime. What I need when its come across this datetime skip it like it does with the weekend. there's the code Code: [Select] <?php$database_date = '2011-08-12, 07:00:00'; //this comes from your database, $updated_time = strtotime('+' . hoursLeft($database_date,7,16,24) . ' hours',strtotime($database_date)); //Handle the date:$new_date = date('m-d-Y, H:i:s',$updated_time); //this is when the email should be sent.echo date('m-d-Y, H:i:s',strtotime($database_date)) . '<br />' . $new_date . '<-- you should be emailing at this time!'; //just echo's it to the screen.function hoursLeft($time,$work_starts,$work_ends,$time_to_email,$skip_weekends = true) { //this function handles the count of how many hours you need to fulfill your task. $parts = explode(' ',$time); //split the time off of the date. list($hour, $minute, $second) = explode(':',$parts[1]); //get the hours, minutes, seconds. if($minute == 0) { //if the minutes is over 0, then return a full hour for it $hours = $work_ends - $hour; } else { //otherwise, take into account that this isn't a full hour. $hours = ($work_ends - 1) - $hour; } $day_count = 0; //original day count is 0; for($i = $work_ends,$count = $hours; $count <= $time_to_email; $i++) { //Start the increment at 16, the count at the current hours, keep the count below 25, increment on each loop. if($i > 24) { //if the increment is over 24, reset it to 0. $i = 0; ++$day_count; //increment day_count when i goes over 24 hour limit. if($skip_weekends == true) { //if you want to skip weekends $day = date('l',strtotime("+ {$day_count} days",strtotime($time))); //get the full text of the current day the hours are pointing to. if(in_array($day, array('Saturday','Sunday'))) { //and current day is in the weekend. $hours += 24; //add 24 hours to the count. continue; //restart loop, before any counting takes place. } } } if($i > $work_starts && $i <= $work_ends) { //if the increment is between 7 and 16, add to the count, which will break the loop at 24. $count += 1; } $hours += 1; //add to the hours, the loop breaks at a count of 24, which will give us the total hours to add to the updated_time above. } return $hours; //return hours}?> OUTPUT: Code: [Select] 08-12-2011, 07:00:0008-16-2011, 14:00:00<-- you should be emailing at this time!9 hours for Friday:9 hours for Monday:7 hours for Tuesday:= 24 hours. I'm waiting for your replys.. I keep getting this error, because people can track their support tickets by using a MD5 encrypted code upon submitting, and I'm wondering if maybe it's too long? Code: [Select] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'by FROM replies WHERE replyto = 'c8c3455c5efadc80b05316c412ab61e6' ORDER BY id D' at line 1 Query: Code: [Select] $replies = mysql_query("SELECT message,by FROM replies WHERE replyto = '$id' ORDER BY id DESC") or die(mysql_error()); can I do this mysql_query("INSERT INTO directory (field, field, field, field,field,field, field,field,field, fieldfield,field, field,field,field,) VALUES ('field','field','field', 'field','field','field', 'field','field','field', 'field','field','field' ,'field','field','field',)"); please ignore any syntax errors and stuff its just the concept of breaking it into different lines? thanks Hello Guys, I need some help here.. I am trying to extract the lat and long from a json query from google. Here is what I receive back from json { "name": "795PollardBlvdSW,Atlanta,GA", "Status": { "code": 200, "request": "geocode" }, "Placemark": [ { "id": "p1", "address": "Atlanta, GA, USA", "AddressDetails": { "Accuracy" : 4, "Country" : { "AdministrativeArea" : { "AdministrativeAreaName" : "GA", "SubAdministrativeArea" : { "Locality" : { "LocalityName" : "Atlanta" }, "SubAdministrativeAreaName" : "Fulton" } }, "CountryName" : "USA", "CountryNameCode" : "US" } }, "ExtendedData": { "LatLonBox": { "north": 33.8231844, "south": 33.6747422, "east": -84.2599230, "west": -84.5160418 } }, "Point": { "coordinates": [ -84.3879824, 33.7489954, 0 ] } } ] } Here is the code I am using $geocode=file_get_contents('http://maps.google.com/maps/geo?output=json&q=795PollardBlvdSW,Atlanta,GA'); $output= json_decode($geocode); $lat = $output->results[0]->geometry->location->lat; $long = $output->results[0]->geometry->location->lng; echo $lat; echo $long; Just not sure how to pull the lat and long out of a json into vars.. Please advise.. Dan hi i need the following done for my site, and i have no idea how cause i suck at php i have a toplist site and i want users to only be able to vote once in 24 hrs, and i want to put a recapcha, i know i need public and private keys i have them also could you please give me a snippet of the finished code instead of explaining it, i tend to learn from from editing code then reading so hers the 2 vote files the first one is where they type the name and click vote, 2nd performs the vote first: Code: [Select] <center><form action="voted.php" method="post"> <table border="0" bordercolor="" style="background-color:" width="400" cellpadding="3" cellspacing="3"> <tr> <td>Site name:</td> <td><input type="text" name="title" /></td> </tr> </table> <input type="submit" value="Vote" ></input></center> </form> and 2nd code: Code: [Select] <?php include("config.php"); mysql_query("UPDATE sites SET votes = votes + 1 WHERE title = '$_POST[title]'"); echo "Voted!"; mysql_close($con); ?> ok i would also like a better control panel, instead of having just site name: password: description: [edit] if it where possible id like to have a cpanel that has site name: password: then redirects to another page where you can edit it, if you can't/wont do this for any reason i will settle for just a peie of code to say "invalid username or password" if details are incorrect so heres my code Code: [Select] <?php include("config.php"); mysql_query("UPDATE sites SET content = '$_POST[content]' WHERE title = '$_POST[title]' AND password = '$_POST[password]'"); mysql_query("UPDATE sites SET link = '$_POST[link]' WHERE title = '$_POST[title]' AND password = '$_POST[password]'"); echo "Site Updated"; mysql_close($con); ?> and the html bit Code: [Select] <center><form action="edit.php" method="post"> <table border="0" bordercolor="" style="background-color:" width="400" cellpadding="3" cellspacing="3"> <tr> <td>Site name:</td> <td><input type="text" name="title" /></td> </tr> <tr> <td>Password:</td> <td><input type="password" name="password" /></td> </tr> <tr> <td>Link to site:</td> <td><input type="text" name="link" /></td> </tr> <tr> <td>Description:</td> <td><textarea name="content" ></textarea></td> </tr> </table> <input type="submit" value="Edit Site" ></input></center> </form> </body> </html> ok i hope you can help, ill try to make this have the best punctuation i can, and please dont tell me how insecure these codes are i have been informed plenty of times |