note 24458 deleted from function.date by victor
| From: | victor@php.net | Date: | Sun, 08 Dec 2002 20:01:16 +0000 |
| Subject: | note 24458 deleted from function.date by victor | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-40532@lists.php.net to get a copy of this message | ||
[Editor's note: for dates within the Unix epoch (1970 to 2038 approx), date("w",
mktime(0,0,0,$month,$day,$year)) will work the same.]
/* this function is used to find the day of the week using the Doomsday Algorithm created by John
Horton Conway
for an explanation of the Doomsday Algorithm go to http://www.interlog.com/~r937/doomsday.html
parameters passed to it are $y = year, $m = month, $d = day of month (all numerical)
it results a numerical weekday where Sunday is 0 or -1 if an invalid month is given to it (something
out of the range of 1-12)
note that the function does work with leap years*/
function findweekday ($y, $m, $d){
$century = substr($y, 0, 2)."00";
if (($century-100) % 400 == "0"){
$cday = "0"; //Sunday
}elseif ($century % 400 == "0"){
$cday = "2"; //Tuesday
}elseif (($century+100) % 400 == "0"){
$cday = "3"; //Wednesday
}elseif (($century-200) % 400 == "0"){
$cday = "5"; //Friday
}
$y2 = substr($y, 2, 2);
$a = floor($y2 / 12);
$b = $y2 % 12;
$c = floor($b / 4);
$doomsday = $a + $b + $c + $cday;
$doomsday = $doomsday % 7;
if ($m == "1"){
if($y % 4 == 0 && $y % 100 != 0 || $y % 400 == 0){
$mdd = "-3";
}else{
$mdd = "-4";
}
}elseif ($m == "2"){
if($y % 4 == 0 && $y % 100 != 0 || $y % 400 == 0){
$mdd = "-6";
}else{
$mdd = "-7";
}
}elseif ($m == "3"){
$mdd = "-7";
}elseif ($m == "4"){
$mdd = "-3";
}elseif ($m == "5"){
$mdd = "-5";
}elseif ($m == "6"){
$mdd = "-1";
}elseif ($m == "7"){
$mdd = "-3";
}elseif ($m == "8"){
$mdd = "-6";
}elseif ($m == "9"){
$mdd = "-2";
}elseif ($m == "10"){
$mdd = "-4";
}elseif ($m == "11"){
$mdd = "-7";
}elseif ($m == "12"){
$mdd = "-2";
}else{
return -1;
}
$dif = $d - $mdd;
$weekday = ($doomsday + $dif) % 7;
return $weekday;
}