note 38719 added to function.mktime
| From: | web at nihongo dot d2g dot com | Date: | Sun, 04 Jan 2004 20:52:49 +0000 |
| Subject: | note 38719 added to function.mktime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-62906@lists.php.net to get a copy of this message | ||
function date_diff($date_from,$date_to)
/*
Calculates difference in days from date_from to date_to, taking into account leap years
if date_from > date_to, the number of days is returned negative
date_from and date_to format is: "dd-mm-yyyy"
It can calculate ANY date difference, for example between 21-4-345 and 11-11-3412
This is possible by mapping any date to the "range 0" dates, as this table shows:
INI END RANGE LEAP YEARS
... ... ... ...
01/01/1920 01/01/1939 -3 5
01/01/1940 01/01/1959 -2 5
01/01/1960 01/01/1979 -1 5
01/01/1980 01/01/1999 0 5 * this is the range used for calculations with mktime
01/01/2000 01/01/2019 1 5
01/01/2020 01/01/2039 2 5
01/01/2040 01/01/2059 3 5
01/01/2060 01/01/2079 4 5
... ... ... ...
*/
{
$date_from_parts = explode('-', $date_from);
$date_to_parts = explode('-', $date_to);
$day_from = $date_from_parts[0];
$mon_from = $date_from_parts[1];
$year_from = $date_from_parts[2];
$day_to = $date_to_parts[0];
$mon_to = $date_to_parts[1];
$year_to = $date_to_parts[2];
//if date_from is newer than date to, invert dates
$sign=1;
if ($year_from>$year_to) $sign=-1;
else if ($year_from==$year_to)
{
if ($mon_from>$mon_to) $sign=-1;
else if ($mon_from==$mon_to)
if ($day_from>$day_to) $sign=-1;
}
if ($sign==-1) {//invert dates
$day_from = $date_to_parts[0];
$mon_from = $date_to_parts[1];
$year_from = $date_to_parts[2];
$day_to = $date_from_parts[0];
$mon_to = $date_from_parts[1];
$year_to = $date_from_parts[2];
}
$yearfrom1=$year_from; //actual years
$yearto1=$year_to; //(yearfrom2 and yearto2 are used to calculate inside the range
"0")
//checks ini date
if ($yearfrom1<1980)
{//year is under range 0
$deltafrom=floor((1999-$yearfrom1)/20)*20; //delta t1
$yearfrom2=$yearfrom1+$deltafrom; //year used for calculations
}
else if($yearfrom1>1999)
{//year is over range 0
$deltafrom=floor(($yearfrom1-1980)/20)*20; //delta t1
$yearfrom2=$yearfrom1-$deltafrom; //year used for calculations
}
else
{//year is in range 0
$deltafrom=0;
$yearfrom2=$yearfrom1;
}
//checks end date
if ($yearto1<1980)
{//year is under range 0
$deltato=floor((1999-$yearto1)/20)*20; //delta t2
$yearto2=$yearto1+$deltato; //year used for calculations
}
else if($yearto1>1999)
{//year is over range 0
$deltato=floor(($yearto1-1980)/20)*20; //delta t2
$yearto2=$yearto1-$deltato; //year used for calculations
}
else
{//year is in range 0
$deltato=0;
$yearto2=$yearto1;
}
//Calculates the UNIX Timestamp for both dates (inside range 0)
$ts_from = mktime(0, 0, 0, $mon_from, $day_from, $yearfrom2);
$ts_to = mktime(0, 0, 0, $mon_to, $day_to, $yearto2);
$diff = ($ts_to-$ts_from)/86400;
//adjust ranges
$diff += 7305 * (($deltafrom+$deltato) / 20);
return $sign*$diff;
}
//I hope this can help
//Any suggestion - comment - advise, feel free to e-mail me
//web@nihongo.d2g.com - Leonardo Sasso
----
Manual Page -- http://www.php.net/manual/en/function.mktime.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+38719
Delete -- http://master.php.net/manage/user-notes.php?action=delete+38719&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+38719&report=yes
Search -- http://master.php.net/manage/user-notes.php