Follow Me

How to calculate number of days between two dates

Sat, Jun 19, 2010

PHP

Following are the script that calculate number of days between two dates. I used this script in my recent project work. I need to run a cron file to all members of the site that haven’t logged into site more than 7 days. I created this script for sending email if log in time of member having more than 7 days.

<?php
	$newupdated_at="2010-06-11";
	$current_date=date('Y-m-d');

	function dateDiff($dformat, $endDate, $beginDate)
	{
		$date_parts1=explode($dformat, $beginDate);
		$date_parts2=explode($dformat, $endDate);
		// put values in this function in  (M,D,Y) format
		$start_date=gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]);
		$end_date=gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]);
		if($end_date - $start_date<0)
		return -($end_date - $start_date);
		else
		return $end_date - $start_date;
	}

	$datediff=dateDiff("-", $current_date, $newupdated_at);
	echo $datediff;
?>

Notice that gregoriantojd function need parameters in m, d, y order. So when you put parts of date in this function then order of parts will be month,date and year.

Enjoy it :)

, , ,
DeliciousFacebookDigg
RSS FeedStumbleUponTwitter

Leave a Reply