| version 1.1, 2002/03/27 23:32:31 |
version 1.2, 2002/03/30 23:45:51 |
|
|
| <?php |
<?php |
| // |
/* vim: set expandtab tabstop=4 shiftwidth=4: */ |
| // +----------------------------------------------------------------------+ |
// +----------------------------------------------------------------------+ |
| // | PHP Version 4 | |
// | PHP Version 4 | |
| // +----------------------------------------------------------------------+ |
// +----------------------------------------------------------------------+ |
|
|
| // $Id$ |
// $Id$ |
| |
|
| /** |
/** |
| * Description. |
* Date_Week_Magic - ISO 8601 implemetation |
| |
* |
| |
* This package implements ISO 8601 specification. It can count week and day |
| |
* number for specified date. It can also count date of Monday for |
| |
* specified week in year. |
| * |
* |
| * Usage: |
* Usage: |
| * <?php |
* <?php |
| * ?> |
* require_once 'Date/Week_Magic.php'; |
| |
* |
| |
* // First example |
| |
* $day = 1; |
| |
* $month = 6; |
| |
* $year = 1981; |
| |
* $ar = Date_Week_Magic::week($year, $month, $day); |
| |
* |
| |
* echo "Date $day/$month/$year was day $ar[weekday] in week $ar[week]."; |
| |
* |
| |
* // Second example |
| |
* $week = 50; |
| |
* $year = 1998; |
| |
* $ar = Date_Week_Magic::monday($year, $week); |
| |
* |
| |
* echo "Monday of week $week in year $year was $ar[day]/$ar[month]."; |
| |
* ?> |
| * |
* |
| * @author Ondrej Jombik <nepto@pobox.sk> |
* @author Ondrej Jombik <nepto@pobox.sk> |
| * @package Credit_Card |
* @package Date_Week_Magic |
| * @version 1.0 |
* @version 0.1 |
| * @access public |
* @access public |
| */ |
*/ |
| class Date_Week_Magic |
class Date_Week_Magic |
| { |
{ |
| |
|
| /** |
/** |
| * Validate the credit card number |
* Get week information for date |
| * |
* |
| * This function checks given credit card number according |
* Method returns associative array with two keys: week and weekday. |
| * Luhn check algorithm. It is published on several places, |
* Key week contain week number (from 1 to 53) and weekday contain week |
| * and also here: |
* day number. First day of week is Monday with value of 1. Sunday |
| * |
* have value 7. 0 is undefined. On error, false is returned. |
| * http://hysteria.sk/prielom/prielom-12.html#3 |
* |
| * |
* @access public |
| * @param string $number credit card number |
* @param int $year year (allowed forms: YYYY, YY) |
| * @return bool true if number is valid, otherwise false |
* @param int $month month (from 1 to 12) |
| */ |
* @param int $day day (from 1 to 31) |
| |
* @return array 'week' => week number |
| /* |
* 'weekday' => week day (1 - Mon, 7 - Sun) |
| * week() |
* on error false is returned |
| * |
*/ |
| * Sets week and weekday according to date in year/month/day. |
function week($year, $month, $day) |
| * Returns non-zero if it failed, zero if OK. |
{ |
| */ |
$timeNowInSeconds = mktime(0, 0, 0, $month, $day, $year); |
| /* Returns non-zero if it failed, zero if OK. */ |
$timeNow = localtime($timeNowInSeconds, 1 /* associative */); |
| function week($year, $month, $day) |
|
| { |
if ((($timeNow['tm_year'] >= 100 && $timeNow['tm_year'] - 100 == $year) |
| // returns: int *aweek, int *weekday) |
|| ($timeNow['tm_year'] < 100 && $timeNow['tm_year'] == $year) |
| |
|| (1900 + $timeNow['tm_year'] == $year)) |
| $timeNowInSeconds = mktime(0, 0, 0, $month, $day, $year); |
&& $timeNow['tm_mon'] + 1 == $month |
| $timeNow = localtime($timeNowInSeconds, 1); |
&& $timeNow['tm_mday'] == $day) |
| |
; // do nothing, date is valid |
| if($timeNow['tm_mon'] + 1 != $month) { |
else |
| //echo "Strange: " .$timeNow['tm_mon'] . " / " . $month . "<br>"; |
return false; |
| //var_dump($timeNow); |
|
| return false; /* this is no longer the same month! */ |
// Function localtime() 'tm_yday' 0 based, we need it 1 based. |
| } |
$YearDay_I = $timeNow['tm_yday'] + 1; |
| |
$yearNow = $timeNow['tm_year']; |
| $yearNow = $timeNow['tm_year']; |
|
| /* struct tm stores yday 0 based, we need it 1 based */ |
// American weeks begin on Sundays. ISO doesn't. */ |
| $YearDay_I = $timeNow['tm_yday'] + 1; |
$WeekDay_I = $timeNow['tm_wday']; |
| |
if (0 == $WeekDay_I) /* if it's Sunday */ |
| /* silly americans begin the week on sundays. ISO doesn't */ |
$WeekDay_I = 7; /* make it Sunday */ |
| $WeekDay_I = $timeNow['tm_wday']; |
|
| if (0 == $WeekDay_I) /* if it's sunday, */ |
$WeekNo_I = intval($YearDay_I / 7); |
| $WeekDay_I = 7; /* make it sunday! */ |
$FirstWeek_I = $YearDay_I % 7; |
| |
|
| //echo "YearDay_I = $YearDay_I<br>"; |
$FirstWeek_I -= $WeekDay_I; |
| $WeekNo_I = intval($YearDay_I / 7); |
if (($FirstWeek_I <= 0) && ($WeekNo_I > 0)) { |
| $FirstWeek_I = $YearDay_I % 7; |
// Break up one week and add the days to FirstWeek_I. |
| |
$WeekNo_I--; |
| $FirstWeek_I -= $WeekDay_I; |
$FirstWeek_I += 7; |
| if (($FirstWeek_I <= 0) && ($WeekNo_I > 0)) { |
} |
| /* break up one week and add the days to FirstWeek_I */ |
|
| $WeekNo_I--; |
if ($FirstWeek_I > (-4)) |
| $FirstWeek_I += 7; |
$WeekNo_I += 1; |
| } |
|
| |
if ($FirstWeek_I > 3) |
| if ($FirstWeek_I > (-4)) |
$WeekNo_I += 1; |
| $WeekNo_I += 1; |
|
| |
if (0 == $WeekNo_I) { |
| if ($FirstWeek_I > 3) |
// Same as previous year's last day. |
| $WeekNo_I += 1; |
|
| |
$ar = Date_Week_Magic::week($year - 1, 12 /* December is 12 */, 31); |
| if (0 == $WeekNo_I) { |
$WeekNo_I = $ar['week']; |
| /* same as previous year's last day: */ |
} |
| |
else if($WeekNo_I > 52) { |
| $res = $this->week($year - 1, 11 /* December is 11 */, 31); |
/* The last days of the year. */ |
| $WeekNo_I = $res['week']; |
if($WeekDay_I < 4 && $day > 28) { |
| // $fool = $res['weekday']; |
/* Three or less days of this year in 53 makes it week 1 instead! */ |
| } |
$WeekNo_I = 1; |
| else if($WeekNo_I > 52) { |
} |
| /* the last days of the year */ |
} |
| if($WeekDay_I < 4 && $day > 28) { |
|
| /* Three or less days of this year in 53 makes it week 1 instead! */ |
return array( |
| $WeekNo_I = 1; |
'week' => $WeekNo_I, |
| } |
'weekday' => $WeekDay_I |
| } |
); |
| |
} |
| return array( |
|
| 'week' => $WeekNo_I, |
/** |
| 'weekday' => $WeekDay_I |
* Get day information for week |
| ); |
* |
| } |
* Returns asssociative array with two keys: day and month. It is day and |
| |
* month of Monday of passed week and year. Note, that the first week can |
| /* |
* begin in the previous year. |
| * week_no() |
* |
| * |
* @access public |
| * Sets monday of week in year to ret_month and ret_day. Year is the same. |
* @param int $year year (allowed forms: YYYY, YY) |
| * If value of week is more than 53, it is truncated. Returns always 0. |
* @param int $week week (from 1 to 53) |
| */ |
* @return array 'day' => day of Monday |
| /* |
* 'month' => month of Monday |
| * Mission: we have year and week number. What date does that week |
* on error false is returned |
| * start with? (i.e what's the date monday that week) |
*/ |
| */ |
function monday($year, $week) |
| function week_no($year, $week) |
{ |
| { |
$day = 15; |
| // returns: int *ret_month, int *ret_day) |
|
| //$month = 0; |
while ($week > 53) |
| $day = 15; |
$week -= 53; |
| |
|
| //int getweek; |
$month = intval((11 * $week) / 53); // assume 53 weeks a year |
| //int getday; |
|
| |
while (1) { |
| while ($week > 53) |
$ar = Date_Week_Magic::week($year, $month + 1, $day); |
| $week -= 53; |
if ($ar == false) { |
| |
$month++; |
| $month = 11 * $week / 53; /* assume 53 weeks a year */ |
$day = 1; |
| |
continue; |
| do { |
} |
| $res = $this->week($year, $month, $day); |
|
| if ($res == false) { |
$getweek = $ar['week']; |
| $month++; |
$getday = $ar['weekday']; |
| $day=1; |
|
| continue; |
if (($getweek > $week - 3) && ($getweek < $week + 3)) { |
| } |
if (($getweek == $week) && ($getday == 1)) { |
| |
break; |
| $getweek = $ar['week']; |
} |
| $getday = $ar['weekday']; |
else { |
| |
if ($day == 1 && $month == 0) |
| /* |
return false; |
| printf("Year %d Month %d Day %d Week %d WDay %d\n", |
|
| year, month+1, day, getweek, getday); |
if ($getweek < $week) { |
| */ |
if ($week - $getweek > 1) { |
| |
$day += 7; |
| if (($getweek > $week - 3) && ($getweek < $week + 3)) { |
} |
| if (($getweek == $week) && ($getday == 1)) { |
else { |
| break; |
$day += (8 - $getday); |
| } |
} |
| else if ($getweek < $aweek) { |
} |
| if ($week - $getweek > 1) { |
else { |
| $day += 7; |
if ($getweek - $week > 1) { |
| } |
$day -= 7; |
| else { |
} |
| $day += (8 - $getday); |
else if ($getweek == $week) { |
| } |
$day -= ($getday - 1); |
| } |
} |
| else { |
else { |
| if ($getweek - $aweek > 1) { |
$day -= ($getday + 6); |
| $day -= 7; |
} |
| } |
|
| else if ($getweek == $week) { |
if ($day <= 0) { |
| $day -= $getday - 1; |
$day = 28; |
| } |
$month--; |
| else { |
} |
| $day -= $getday + 6; |
} |
| } |
} |
| |
} |
| if ($day <= 0) { |
else { |
| $day = 28; |
if(++$month > 11) { |
| $month--; |
return false; |
| } |
} |
| } |
} |
| } |
} |
| else { |
|
| if(++$month > 11) { |
return array( |
| $day = 0; |
'day' => $day, |
| $month = 0; |
'month' => $month + 1 |
| break; |
); |
| } |
} |
| } |
|
| } while(1); |
|
| |
|
| return array( |
|
| 'day' => $day, |
|
| 'month' => $month + 1 |
|
| ); |
|
| } |
|
| |
|
| } |
} |
| ?> |
|
| |
|
| |
?> |