PHP scripts and programs for Beginner to Master

php programs, php ready to use scripts & functions, css tutorials, html, javascripts, ajax examples for Beginner to Master

How can we know the number of days between two given dates using MySQL ? or find the difference between dates using MySQL ?

Do you ever faced this question in technical interview or technical test?

I know many of you have faced this....... :)

Here is a simple function which can be used to find the number of days between two given dates in MySQL.

Syntax - DATEDIFF(firstdate,seconddate)

so internally it will calculate the difference in MySQL by doing "firstdate minus seconddate", so if firstdate is greater than sencoddate then output will be positive and if firstdate is less than seconddate then output will be negative.

eg. if you use this function as

SELECT DATEDIFF('2009-11-19', '2009-10-15');

in MySQL
then you will get output as 35.

And if you pass parameters as

SELECT DATEDIFF('2009-10-15','2009-11-19')

then you will get output as -35.


If you know any other way to find the difference between dates then comments are always welcome.
Dont forget to tell your friends about http://programming-in-php.blogspot.com

Many times when you go for an interview or technical test you may find this question 

"What is the difference between the sort() and asort() function in php ?"

Basically sort() and asort() functions are used to sort the array in php.

* sort() function syntax - sort(array,sorttype)


sorttype is Optional. Specifies how to sort the array values.

* SORT_REGULAR - Default. Treat values as they are (dont change types)
* SORT_NUMERIC - Treat values numerically
* SORT_STRING - Treat values as strings
* SORT_LOCALE_STRING - Treat values as strings, based on local settings



This function assigns new keys for the elements in the array. Existing keys will be deleted.

eg. See the code below when we create array $myarray we have assigned keys as a b & c.
Now we are applying sort function and printing the array.





so the output of the above code will be -
initial array contents -

Array ( [a] => chaprak [b] => programming [c] => dexter )

and after applying sort function -

Array ( [0] => chaprak [1] => dexter [2] => programming )

its keys are changed to numeric values and sorting is done.

View php manual for the array sort function


* asort() function syntax - asort(array,sorttype)

The asort() function sorts an array by the values. The values keep their original keys (index).


sorttype is Optional. Specifies how to sort the array values.

* SORT_REGULAR - Default. Treat values as they are (don't change types)
* SORT_NUMERIC - Treat values numerically
* SORT_STRING - Treat values as strings
* SORT_LOCALE_STRING - Treat values as strings, based on local settings






when we execute above code then it will produce the following output

initially array contents are - Array ( [a] => chaprak [b] => programming [c] => dexter )

after applying asort() function the contents are -

Array ( [a] => chaprak [c] => dexter [b] => programming )


see here sorting is done and even index is maintained.

View php manual for the array sort function