Count Days Between Two Dates Functions in Javascript and PHP

I need Count Days Between Two Dates Functions in Javascript and PHP just several hours ago, while doing my project. Because this function is not a predefined function either in Javascript or PHP, then I think it is better to share this function to you.

Like its name, Count Days Between Two Dates Functions in Javascript and PHP is used to calculate how many days between two given dates. I write these functions in Javascript and PHP languages. However, the bassic logic of these two function is the same.

Lets just jump to the first function that is written in Javascript language.

function count_days(date1, date2) {
	var each_day = 1000 * 60 * 60 * 24;//milliseconds in a day
	var ms_date1 = date1.getTime();//milliseconds for date1
	var ms_date2 = date2.getTime();//milliseconds for date2
	var ms_date_diff = Math.abs(ms_date1 - ms_date2);//different of the two dates in milliseconds
	var days = Math.round(ms_date_diff / each_day);//divided the different with millisecond in a day
	return days;
}

Then lets just see the second function that is written in PHP language.

function count_days($date1, $date2){
	$each_hours = 60 * 60 * 24;//seconds in a day
	$s_date1 = strtotime($date1);// seconds for $date1
	$s_date2 = strtotime($date2);// seconds for $date2
	$s_date_diff = abs($s_date1 - $s_date2);//different of the two dates in seconds
	$days = round($s_date_diff / $each_hours);//divided the different with second in a day
	return $days;
}

The logic behind the function:

  1. Find how many seconds/milliseconds in a day
  2. Convert the two dates into seconds/milliseconds
  3. Find the different between the two dates in seconds/milliseconds
  4. Make an absolute value of the different
  5. Because the different is in seconds/milliseconds, then we need to divided the different with seconds/milliseconds in a day
  6. Return the final result

You can call the Javascript functions like:

var d1 = new Date(2010,4,12);
var d2 = new Date(2011,4,12);
document.write('There are '+count_days(d1,d2)+' days, between 12-04-2010 and 12-04-2011.(Calculated using javascript)');

You can call the PHP functions like:

echo 'There are '.count_days("2010-4-12","2011-4-12").' days, between 12-04-2010 and 12-04-2011.(Calculated using php)';

Both of the function will give the exactly same result that is 365 days.

That is it about Count Days Between Two Dates Functions in Javascript and PHP. Feel free to copy and use Count Days Between Two Dates Functions in Javascript and PHP in your project.

http://septiadi.com/2011/04/12/count-days-between-two-dates-functions-in-javascript-and-php/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: