User Tools

Site Tools


php:kompost:kalenderwochen

[PHP] Kalenderwochen

Ausgangslage

PHP unter Window liefert die Kalenderwoche nicht nach ISO 8601:1988 so wie wir es in Zentraleuropa gerne hätten. Darum muss ein Workaround her

Lösung

<?php
	setlocale(LC_TIME, 'de_DE', 'deu_deu');  
	$today = time();
	$yearend = mkdate(12, 31, date('Y'));
 
	$monday = strtotime("last Monday", $today);
	while ($monday <= $yearend){
		$date = new stdClass();
		$date->nr = getWeekNr($monday);
		$date->monday = $monday;
		$date->mondayStr = strftime("%x", $monday);
		$date->sunday = strtotime("next Sunday", $monday);
		$date->sundayStr = strftime("%x", $date->sunday);
		$weeks[] = $date;
		$monday = strtotime("+1 week", $monday);        
	}
 
	//Testausgabe
	foreach($weeks as $week){
		echo "{$week->nr}: {$week->mondayStr} bis {$week->sundayStr}<br />\n";
	}
 
	/**
	 * create a date without time
	 * @param int $month
	 * @param int $day
	 * @param int $year
	 * @return timestamp
	 */
	function mkdate($month, $day, $year){return mktime(0, 0, 0, $month, $day, $year);}
 
 
	/**
	 * date('W') liefert für uns die falsche Wochennummer. Wir wollen nach ISO 8601:1988
	 * (erste Woche = Woche mint mindestens 4 Tage des neuen Jahres
	 * @param timestamp $date
	 * @return int
	 */
	function getWeekNr($date){
		//Windows kann icht alle Formatierbefehle. darum muss die Woche nach ISO 8601:1988 format anderst hergeholt werden
		//Version für Windows:
		if (stristr(PHP_OS, "win")<>false){
			return getWeekNumber(date('j', $date), date('n', $date), date('Y', $date));
		} else {
		//Version für Unix:
			return (Integer) strftime('%V', $date);
		}
	}
 
	// [url]http://theserverpages.com/php/manual/en/function.strftime.php[/url]
	// Get the week number in ISO 8601:1988 format
	// This helsp when %V is not working on your Win32 machine
	function getWeekNumber($day, $month, $year) {
	 $week = strftime("%W", mktime(0, 0, 0, $month, $day, $year));
	 $yearBeginWeekDay = strftime("%w", mktime(0, 0, 0, 1, 1, $year));
	 $yearEndWeekDay  = strftime("%w", mktime(0, 0, 0, 12, 31, $year));
	 // make the checks for the year beginning
	 if($yearBeginWeekDay > 0 && $yearBeginWeekDay < 5) {
	  // First week of the year begins during Monday-Thursday.
	  // Currently first week is 0, so all weeks should be incremented by one
	  $week++;
	 } else if($week == 0) {
	  // First week of the year begins during Friday-Sunday.
	  // First week should be 53, and other weeks should remain as they are
	  $week = 53;
	 }
	 // make the checks for the year end, these only apply to the weak 53
	 if($week == 53 && $yearEndWeekDay > 0 && $yearEndWeekDay < 5) {
	  // Currently the last week of the year is week 53.
	  // Last week of the year begins during Friday-Sunday
	  // Last week should be week 1
	  $week = 1;
	 }
	 // return the correct ISO 8601:1988 week (in two digit format)
	 return( substr('0'. $week, -2) );
	}
?>

CatPhp

php/kompost/kalenderwochen.txt · Last modified: 11.12.2013 11:18:30 (external edit)