PHP - What's The Difference Between A Method And Function?
is this calling a function or calling a method? what's the difference?
Code: [Select] $thedatabase->opendb(); Code: [Select] class Database { function opendb() { $this->connect= mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$this->connect) { die (mysql_error()); } $db_select = mysql_select_db(DB_NAME, $this->connect); if(!$db_select) { die (mysql_error()); } } } Similar Tutorialstry to give difference in points Hi Guys
I wonder if someone can explain something that is probably quite basic.
When I try to add an event listener and target a normal function this works fine - e.g. 'window.addEventListener('load',test)'. However, if I try to target a function that is a property of an object this fails - e.g. 'window.addEventListener('load',initialiser.init)'. Further in this latter case I also get a console log saying it's undefined.
The code for this example is below.
So my question is why does it fail onload when I use a function in an object as opposed to a straight function? Presumably it's something to do with the way the document and the code are loaded or initialised but what is it that makes them behave differently.
Thanks,
Drongo
<script> //this function call works fine //window.addEventListener('load',test); //this functiton call fails and is undefined in console window.addEventListener('load',initialiser.init); //example object holding a method var initialiser = { init:function(){ alert('Init called!'); } }; //some random function function test(){ alert('test called!'); } </script> Edited by Drongo_III, 18 May 2014 - 10:59 AM.
My script has 3 classes (that are relevant to this discussion): DB, User and Validate. They are all in independent files and loaded automatically, when required, by an autoloader.
The error messages I am getting a Any pointers as to what I am doing wrong, or what I should be doing, would be most welcome. I am working with wordpress and I see a lot of "if" statements done like this if(condition): do something endif; I have always done it like so if(condition) { do something } what is the difference? does the first method have any advantages over the second method? This is going to seem like a stupid question but I'm curious about it anyway. Isn't isset and !empty do the same? Considering security, is there a disadvantage in using $_REQUEST? Hi, In Php , What is the different ebtween abc() and @abc() ??? Please advice . Actually what's the difference between Code: [Select] if(...) and Code: [Select] if(isset(...) <?php $i = 2; $i = $i++; echo $i; ?>Output of above code is 2. <?php $i = 2; $p = $i++; echo $i; ?>Output of above code is 3. Why this difference? I mean how these codes are processed to give an output? Hey guys im stuck in a bind and need some input. What im trying to do is display a number based from community levels. ex: This user need another 2 tasks to get next level. here are the levels 1- 0-2 task new 2- 2-5 tasks beginer 3- 5-7 tasks pro 4- 7+ tasks advanced The way it works is by two different tasks. basicly i wanna know which is the higher number in the tasks either how many posts or comment and figure out based on our levels how many tasks he need to complete in order for him to reach the next level. the first query sees how many post this user has the second check how many comment he has I wanna take the highest number and that belongs to whicheber catgory, do a calculation. and have this kind of output This user is level 2 and need 1 more task to reach the next level. Like i know if he need one more that he has 4 tasks done. I know im all over the place with this one im just trying to get someone who might have an idea of my issue and point me in the right direction. Either if this is a known ?? does this procedure has a specific name anything will help me. Problem; Need to check to see if someone who requested a password re-set has done so with in 24hrs. IF not- then it needs to prompt them to re-submit the request and deletes the previous re-submit code sent to them in their email. I have everything else figured out (deleting re-set code) but I have not figured the checking of if they have done so within the 24hr time period. Please tell me if the code below will accurately tell me if they complied with the 24 hour rule. $time1 = '1284675867';// logged at the time of password re-set request. Retrieved from database $time2 = time();// captured in script when they actually re-set their password $difference_between = $time2 - $time1; if($difference_between >= '5,200,000') { echo "Sorry- you have exceeded the 24 hr rule and need to re-submit your request"; } Compared to all that I have seen.........it appears rather simplistic. Am I missing something or will this simple approach work? i have searched everywhere and no code seems to work. i have time1 and time 2 in the format Y-m-d H:i:s. i just don't know how to find the difference. old time: 2011-04-11 20:15:46 new time: 2011-04-11 21:00:11 after i have the difference, i need to get a resource production per hour which is stored in the database and calculate the new value of the resource. $production_per_hour = 100; $time_old = "2011-04-11 20:15:46"; $time_new = "2011-04-11 21:00:11"; $current_resources = "500"; Basically I am using DATETIME to store the time in which a row was input. Format - 2010-11-13 21:31:53 How would I get the current time of page load and compare it to the database row "2010-11-13 21:31:53" then output the difference. Example $sqlresult = 2010-11-13 21:31:53; $currentTime = (GET CURRENT DATE+TIME = (2010-11-13 21:31:55) ); Output - This was posted 2 minute's ago. Thank you in advance. Any help would be greatly appreciated. Hi, I am trying to get the difference in time in mins, hours etc thingy. Now, for some reason I cannot get the right minutes or hours difference between two time stamps. Here is my code: Code: [Select] $postedTime=strtotime($comment_from_db->date_posted); $currentTime=time(); $mins=floor(($currentTime-$postedTime)/60); if($mins<=1){ echo $mins." minute ago by"; }elseif($mins>1){ if($mins>60){ $hrs=floor($mins/60); if($hrs>24){ }else{ if($hrs<2){ echo $hrs. " hour ago by"; }else{ echo $hrs. " hours ago by"; } } }else{ echo $mins." minutes ago by"; } } Now, for a comment posted like 25 mins ago it shows it was posted 4 hours ago. For a comment posted around 10 hours ago it shows it was posted 16 hours ago. What is the problem here? I was doing a test and checked the time in seconds after an interval of 2 mins. Here are the results: Comment posted: 1306669490 Current time: 1306683920 The difference is around 14430. This is not equal to 2 minutes. So, I am starting to think if the time I am getting is wrong? Should it have 10 digits? or less? I am not sure what is going on. Please, advice. Thanks. For some reason when I echo $diff and $time_remaining it ALWAYS echos with values of 3 and 7 and not sure why. Code: [Select] $lock_date = $row['lock_date']; $current_time = time(); // Find out if user is locked out of their account if (($lock_date != "0000-00-00 00:00:00") && strtotime($lock_date) < $current_time) { $lock_date = strtotime($lock_date); $diff = $current_time - $lock_date; $diff = floor($diff/6000); // Take minutes and perform tasks if ($diff <= 10) { // Calculate time remaining $time_remaining = 10 - $diff; // Account locked error $errors = true; $message = "Account is locked! Must wait " .$time_remaining." minutes to log in again!"; $output = array('errorsExist' => $errors, 'message' => $message); } else { // Clear the lock $query = "UPDATE users_logins_attempts SET lockDate = NULL, ip_address = NULL, failed_logins = 0 WHERE users_id = '".$users_id."'"; $result = mysqli_query($dbc,$query); // Account locked error $errors = true; $message = "Account is unlocked. You may now try to log in again!"; $output = array('errorsExist' => $errors, 'message' => $message); } } Hi I'm trying to get this to work. I have known date in MySQL database entered as 2019-08-01 and I need to calculate from that date to current time. What I have is this code $date_ts = strtotime($row['date_ts']); //Timestamp $date_str = date("M-d-Y", $date_ts); //String in format MMM-DD-YYYY $TotalTime = floor((time() - $date_ts)/(60*60*24)) . ' days';//Total in days but my result showing as ==> 18367 days Could someone please help me get this to work so it shows the right number of days. thank you
I'm trying to subtract get the total number of days between two dates. I have been trying the DateTime date_diff method and the strtotime methods. I have it working mostly; but I have a problem with if the value should be negative. I've tried the invert() on the value that comes back from the date_diff() function but get errors. To sum up what I'm trying to do is:
if ( $diff === 0 ) //retrieve expected complete time if ( isset( $stat[ 'expected_complete_time' ] ) ) { $today = new DateTime("now"); $expected_complete_time = new DateTime($stat[ 'expected_complete_time' ]); $today->setTime(12, 0, 0); $expected_complete_time->setTime(12, 0, 0); //calculate difference $eta = date_diff($expected_complete_time, $today)->format("%d");; //future if ($today > $expected_complete_time) { $eta = '+' . $eta; //today } elseif ($eta === '0') { $eta = 'Today'; //overdue } else { $eta = 'LATE:-' . $eta; } } else $eta = '';
hiii, Im wanting to calculate the time difference between $out2 and $in2 and out put it into a spreadsheet: Code: [Select] $m=0; while ($m < $num9) { $fullname=mysql_result($result9,$m,"username"); $date=mysql_result($result9,$m,"date"); $in1=mysql_result($result9,$m,"in1"); $out1=mysql_result($result9,$m,"out1"); $in2=mysql_result($result9,$m,"in2"); $out2=mysql_result($result9,$m,"out2"); $out2f=date("G:i:s",strtotime($out2)); $in2f=date("G:i:s",strtotime($in2)); echo date("D",strtotime($date)). "\t" . $fullname . "\t" . $in1 . "\t". $out1 . "\t" . $in2 . "\t" . $out2 . "\t" . date("G:i:s",strtotime($out2f-$in2f)) . "\t" . "\n"; $m++; } header("Content-disposition: attachment; filename=spreadsheet.xls"); why does this code return 00:00:00 ? This topic has been moved to HTML Help. http://www.phpfreaks.com/forums/index.php?topic=315426.0 |