Week functies
Gepost door Vincent V op 12-09-2010 15:22.
Ik heb hieronder 3 functie, verdere beschrijving ervan vind je terug boven iedere functie. Aangezien dit niet is ingebouwd in php en ik het nodig had, heb ik het gemaakt. Foutcontrole is ingebouwd.
Hopelijk zijn jullie er iets mee.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | /**
* @author Vincent Verweij
* @copyright 2010-09-12
*/
/**
* This function gives back how many weeks there are in the given month
* @param $month int
* @param $year int
* @return $week total weeks of month
* @return false on failure
**/
function GetTotalWeeksOfMonth($month, $year)
{
//check for wrong input
if(($month < 1) || ($month > 12))
return false;
if(($year < 0) || ($year > 9999))
return false;
//initialize
$daysInMonth = date("t", mktime(0, 0, 0, $month, 1, $year));
$week = 1;
//search for total weeks
for ($i = 1; $i <= $daysInMonth; $i++)
{
$currentDayName = date("l", mktime(0, 0, 0, $month, $i, $year));
if (($currentDayName == 'Monday') && ($i != 1))
$week++;
}
return $week;
}
/**
* This function gives back the current week you're in at the given date
* @param $day int
* @param $month int
* @param $year int
* @return $week current weeknumber in month
* @return false on failure
**/
function GetWeekOfMonth($day, $month, $year)
{
//check for wrong input
if (($month < 1) || ($month > 12))
return false;
if (($year < 0) || ($year > 9999))
return false;
$daysInMonth = date("t", mktime(0, 0, 0, $month, 1, $year));
if (($day < 1) || ($day > $daysInMonth))
return false;
//initialize
$dayNameNeeded = date("l", mktime(0, 0, 0, $month, $day, $year));
$week = 1;
$i = 1;
//search for current week of month
while ($i <= $day)
{
$currentDayName = date("l", mktime(0, 0, 0, $month, $i, $year));
if (($currentDayName) == 'Monday' && ($i != 1))
$week++;
$i++;
}
return $week;
}
/**
* This function gives back an array. For example every saturday if you give '2010-09-04'
* @param $day int
* @param $month int
* @param $year int
* @param $week int
* @return $array array with keys for each month and date values of every given week
* @return false on failure
**/
function GetEveryMonthDate($day, $month, $year, $weekNeeded)
{
//initialize
$dayNameNeeded = date("l", mktime(0, 0, 0, $month, $day, $year));
$array = array();
//check for wrong input
if (($month < 1) || ($month > 12))
return false;
if (($year < 0) || ($year > 9999))
return false;
$daysInMonth = date("t", mktime(0, 0, 0, $month, 1, $year));
if (($day < 1) || ($day > $daysInMonth))
return false;
$totalWeeks = GetTotalWeeksOfMonth($month, $year);
if ($week > $totalWeeks)
return false;
//loop for the array
for($m = 1; $m <= 12; $m++)
{
//reset values
$week = 1;
$daysInMonth = date("t", mktime(0, 0, 0, $m, 1, $year));
$dateFound = false;
$i = 1;
//correct date search
while ($i <= $daysInMonth && !$dateFound)
{
$currentDayName = date("l", mktime(0, 0, 0, $m, $i, $year));
$currentMonth = date("M", mktime(0, 0, 0, $m, $i, $year));
if (($currentDayName) == 'Monday' && ($i != 1))
$week++;
if ($week == $weekNeeded)
{
if ($currentDayName == $dayNameNeeded)
{
if($m < 10)
$m = '0'.$m;
if($i < 10)
$i = '0'.$i;
$correctDate = "$year-$m-$i";
$array[$currentMonth] = $correctDate;
$dateFound = true;
}
}
$i++;
}
}
return $array;
} |
Bestanden van dit script
functions.php
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | <?php
/**
* @author Vincent Verweij
* @copyright 2010-09-12
*/
/**
* This function gives back how many weeks there are in the given month
* @param $month int
* @param $year int
* @return $week total weeks of month
* @return false on failure
**/
function GetTotalWeeksOfMonth($month, $year)
{
//check for wrong input
if(($month < 1) || ($month > 12))
return false;
if(($year < 0) || ($year > 9999))
return false;
//initialize
$daysInMonth = date("t", mktime(0, 0, 0, $month, 1, $year));
$week = 1;
//search for total weeks
for ($i = 1; $i <= $daysInMonth; $i++)
{
$currentDayName = date("l", mktime(0, 0, 0, $month, $i, $year));
if (($currentDayName == 'Monday') && ($i != 1))
$week++;
}
return $week;
}
/**
* This function gives back the current week you're in at the given date
* @param $day int
* @param $month int
* @param $year int
* @return $week current weeknumber in month
* @return false on failure
**/
function GetWeekOfMonth($day, $month, $year)
{
//check for wrong input
if (($month < 1) || ($month > 12))
return false;
if (($year < 0) || ($year > 9999))
return false;
$daysInMonth = date("t", mktime(0, 0, 0, $month, 1, $year));
if (($day < 1) || ($day > $daysInMonth))
return false;
//initialize
$dayNameNeeded = date("l", mktime(0, 0, 0, $month, $day, $year));
$week = 1;
$i = 1;
//search for current week of month
while ($i <= $day)
{
$currentDayName = date("l", mktime(0, 0, 0, $month, $i, $year));
if (($currentDayName) == 'Monday' && ($i != 1))
$week++;
$i++;
}
return $week;
}
/**
* This function gives back an array. For example every saturday if you give '2010-09-04'
* @param $day int
* @param $month int
* @param $year int
* @param $week int
* @return $array array with keys for each month and date values of every given week
* @return false on failure
**/
//TODO geeft array terug van january tot december met juiste data, grote M voor in array
function GetEveryMonthDate($day, $month, $year, $weekNeeded)
{
//initialize
$dayNameNeeded = date("l", mktime(0, 0, 0, $month, $day, $year));
$array = array();
//check for wrong input
if (($month < 1) || ($month > 12))
return false;
if (($year < 0) || ($year > 9999))
return false;
$daysInMonth = date("t", mktime(0, 0, 0, $month, 1, $year));
if (($day < 1) || ($day > $daysInMonth))
return false;
$totalWeeks = GetTotalWeeksOfMonth($month, $year);
if ($week > $totalWeeks)
return false;
//loop for the array
for($m = 1; $m <= 12; $m++)
{
//reset values
$week = 1;
$daysInMonth = date("t", mktime(0, 0, 0, $m, 1, $year));
$dateFound = false;
$i = 1;
//correct date search
while ($i <= $daysInMonth && !$dateFound)
{
$currentDayName = date("l", mktime(0, 0, 0, $m, $i, $year));
$currentMonth = date("M", mktime(0, 0, 0, $m, $i, $year));
if (($currentDayName) == 'Monday' && ($i != 1))
$week++;
if ($week == $weekNeeded)
{
if ($currentDayName == $dayNameNeeded)
{
if($m < 10)
$m = '0'.$m;
if($i < 10)
$i = '0'.$i;
$correctDate = "$year-$m-$i";
$array[$currentMonth] = $correctDate;
$dateFound = true;
}
}
$i++;
}
}
return $array;
}
?> |
Commentaar
12-09-2010 15:23
13-09-2010 11:51
14-09-2010 19:22
15-09-2010 00:50
19-09-2010 17:55
20-09-2010 20:54
26-09-2010 10:32
27-09-2010 00:04


