Page 1 of 1

datediff

Posted: Tue Jan 17, 2006 6:57 pm
by spitfire_esquive
does anyone know of a php function or script out there that would mimic datediff? *its a function that calculates the difference between two dates to day, minutes, seconds*. or should i try pear?

thank you! :D

Posted: Tue Jan 17, 2006 6:58 pm
by John Cartwright
This isn't a Code Snipplet..

Moved to PHP-Code.

Posted: Wed Jan 18, 2006 5:29 am
by raghavan20
This should help you calculate the number of days between two dates...be careful about formats to be used.

Code: Select all

<?php
/**
Provide from and to dates
While providing dates you can use any of these two formats:
format 1: mm/dd/yyyy
format 2: 10 September 2000
**/

$from_date = "9/9/1998";
$to_date = "10 September 1999";
echo "<br />".floor((strtotime($to_date) - strtotime($from_date))/(60*60*24))."<br />";
dateDifference($from_date, $to_date);
?>

<?php
function dateDifference($from_date, $to_date){
	echo "<br />Number of days: ".floor((strtotime($to_date) - strtotime($from_date))/(60*60*24))."<br 

/>";
}
?>

Posted: Wed Jan 18, 2006 7:11 am
by dude81
If you Can pass tow dates as arguments with time to the functiona and parse them within the function you can get best results
.Though code is long it is works very fine.

I used strtotime but it gave once a problem when Im dealing with it.
It has some problem with the starting day of a everynew year. So I developed this complex function

Code: Select all

function datediff(){
$opening_time =  mktime(OpeningHourHere, Openingminute, OpeningSeciondHere, OpeningmonthHere, OpeningDayhere, OpeningYearhere, Standardoftimehere);
$close_time =  mktime(ClosingHourHere,Closingminuteminute,........,Standardoftime ]);;
$diff = ceil(($opening_time-close_time) / (1 * 24 * 60 * 60));
return $diff;
}

Posted: Wed Jan 18, 2006 7:16 am
by m3mn0n
http://php.net/date has some helpful code snippets in the user comments.

Posted: Wed Feb 08, 2006 3:21 am
by Benjamin
Nevermind. I didn't realize strtotime took leap years into account.