Friday, May 23, 2008

No of days between two dates in php

We can find the no days between two dates using PHP....
There are 2 methods:

1) Split the date and subtract
$date1 = "2008-05-18";
$arr = explode('-',$date1); //split the date
$date2 = $arr[2]; //stores 18
$cur_date = date("d"); // current date
echo "Difference: ";
echo $cur_date-$date2;



2) Convert dates into timestamp, subtract and then convert as we want
$date1 = "2008-04-18";
$to_day = date("Y-m-d"); //current date
$day_diff = (strtotime($to_day)-strtotime($date1))/86400; //60*60*24=86400
echo $day_diff;

//change the divider(864000 to get ur corresponding difference
$hour_difference = (strtotime($to_day)-strtotime($date1))/3600; //60*60=3600
$week_diff = (strtotime($to_day)-strtotime($date1))/604800; //60*60*24*7=604800

No comments: