How can we know the number of days between two given dates
using PHP?

Answer Posted / sachin

If we know the date format and separator used in dates,
we can separate them out into day, month and year.
e.g.

$d1 = '29-12-1985'; //dd-mm-yyyy
$d2 = '31-12-1985'; //dd-mm-yyyy

$d1_pieces = explode('-',$d1);
$d2_pieces = explode('-',$d2);

$timestamp_d1 =
mktime(0,0,0,$d1_pieces[1],$d1_pieces[0],$d1_pieces[2],);

$timestamp_d2 =
mktime(0,0,0,$d2_pieces[1],$d2_pieces[0],$d2_pieces[2],);

$diff = $d1-$d2;

$num_days = $diff/(24*60*60);

Is This Answer Correct ?    3 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the purpose of break and continue statement?

536


Is array function in php?

546


What is a query in php?

488


What does $globals mean?

534


What is the purpose of pear in php?

598






Explain the types of functions for Splitting String?

582


How many php functions are there?

562


What are the different components used in php for formatting?

548


What is use of header() function in php?

560


Which is not a file-related function in php?

624


Can we run php on tomcat server?

576


What is use of htmlspecialchars php?

528


Explain Whitespace Characters?

593


Tell me what is the difference between ereg_replace() and eregi_replace()?

562


What is difference between ksort() and usort() functions.

533