How to compare dates?
The best bet to compare dates is to convert them to intergers. Functions such as time() and strtotime() comes in very handy as they return the date as the number of seconds since the Epoch. Very useful is the the function strtotime() which, when given a string, converts that to date and return that timestamp.
<?php
$t1 = time(); // Assume this correspond to January 1, 2005 at 5PM
$t2 = strtotime('January 1, 2005');
$t3 = strtotime('January 1, 2005 5PM');
$t4 = strtotime('2005-01-01 5PM');
// All of the following should be true
var_dump($t1 > $t2);
var_dump($t1 == $t3);
var_dump($t1 == $t4);
var_dump($t3 == $t4);
?>
How to make parameters optional?
In PHP, when calling a function, PHP will only return a warning if you give it less input than it expected. If you give it more input then it expected, it will still send the extra input(s) to the function body. You can get the number of given inputs by using the function func_num_args(). Likewise, you can get an array of the given inputs by using the function func_get_args(). If you want to make certain parameters optional, then all you need to do is specify its default value when you are declaring the it. Yet another way is to have it accept an array with the optional parameters. The benefit of that is you don't need to remember the order in which the optional parameters should be given but all you need to remember is what is their name. Below are some examples:
// This is a regular function that requires 2 parameters
function myFunc1($fname, $lname) { ... }
// This is a function that requires 2 parameters but the rest are optional
function myFunc2($fname, $lname, $address = 'NA', $phone = 'NA') { ... }
// This is a function that can accept any number of paramters
function myFunc3() {
// Get the parameters
$inputs = func_get_args();
// Do something wtih given parameters
... $inputs['some_key'] ...
}
// This is a function using an array to handle inputs
function myFunc4($inputs) {
$fname = $inputs['fname'];
$lname = $inputs['lname'];
...
}
// Combining the types
function myFunc5($fname, $lname, $optionals = array()) {
// Default values
$defaults = array('address' => 'NA', 'phone' => 'NA');
// Merge defaults with optionals
$oinputs = array_merge($defaults, $optionals);
// Do Something
...
}
I am not getting anything from my database, help!
I see so many people asking this question that I have decided to add this section here. Though I won't be able to fully answer any problems you may have, what I can do is provide a template you could use to help debug and find out the error for the SQL command. I will be using the mysqli_ family of functions but for the most part, it works the same as the old mysql functions.
<?php
// First, connect to the database
$mysqli = new mysqli('host', 'username', 'password', 'database');
// Create the SQL query string
$sql = sprintf(
'SELECT * FROM %s WHERE id="%s" LIMIT 2;',
$table, $id
);
// Query the sql
$query = $mysqli->query($sql);
// Make sure the query was successful
if($query) { // Query was successful
/* Do something with the query */
// Example one: just ouput a message saying that it was successful
echo 'Query successful!';
// Example two: display the info
while($row = $query->fetch_assoc()) {
printf('User ID: %s<br />Full Name: %s', $row['id'], $row['name']);
}
} else { // Query failed
exit(sprintf(
'<pre>SQL: %s<br />Error: %s</pre>',
$sql, $mysqli->error
));
}
?>
I make the habit of always using printf() or sprintf() so that I can keep my SQL clean and easier to maintain. Basically, what the code above does is that it makes the query but that query will return a few variations of result. However, it will always return FALSE if there was something wrong with the SQL. All other return values evaluate to TRUE. That way, if there is an error in my SQL, I can see what that SQL was and look at the error message provided by the MySQL server.