User Tools

Site Tools


php:functions:date:funktionen

[PHP] Erweiterte Datumsfunktionen

Einige Funktionen rund um Datum

Testaufruf der Funktionen

<?php
$date = time();
$firstDay = firstDayOfMonth($date);
$lastDay = lastDayOfMonth($date);
$weekNr = getWeekNr($date);
 
echo 'Eingabedatum: '.date("d.m.Y - H:i:s", $date).'<br />';
echo 'Erster Tag des Monats: '.date("d.m.Y - H:i:s", $firstDay).'<br />';
echo 'Letzter Tag des Monats: '.date("d.m.Y - H:i:s", $lastDay).'<br />';
echo 'Letzter Tag des Monats (Nur Datum): '.date("d.m.Y - H:i:s", truncDate($lastDay)).'<br />';
echo 'Woche: '.$weekNr.'<br />';

lastDayOfMonth

lastDayOfMonth.php
/**
 * letzter Tage des Monats 
 * @param timestamp
 * @return timestamp
 */
function lastDayOfMonth($date){
	//Zitat von PHP.net zu mktime: 
	//'Der letzte Tag eines gegebenen Monats kann als Tag "0" des folgenden 
	//Monats ausgedrückt werden, nicht jedoch als Tag "-1"'
	return mktime(0, 0, 0, date('m', $date)+1, 0, date('Y', $date));
}

firstDayOfMonth

firstDayOfMonth.php
/**
 * erster Tag des Monats
 * @param timestamp
 * @return timestamp
 */
function firstDayOfMonth($date){
	return mktime(0, 0, 0, date('m', $date), 1, date('y', $date));
}

truncDate

truncDate.php
/**
 * kürzt den timestamp auf das Datum
 * @param timestamp
 * @return timestamp
 */
function truncDate($date){
	return mktime(0, 0, 0, date('m', $date), date('d', $date), date('Y', $date));
}

erweiterte Function

truncDate.php
/**
 * @example     truncDate($date[, $min[, $max]]);
 * @param $date Date    Datum das gekürzt werden sollte
 * @param $min  String  Schlüssel der unteren Limite
 * @param $max  String  Schlüssel der obeen Limite
 * @return      Date    gekürztes Datum
 */
function truncDate($date, $min = 'mday', $max = 'year'){
	$order = array('seconds', 'minutes', 'hours', 'mday', 'mon', 'year',);
	$minId = array_search($min, $order);   
	$maxId = array_search($max, $order);   
 
	$dateInfo = getdate($date);
 
	$keys = array_flip(array_slice($order, $minId, $maxId-$minId+1, true));
	$parts = array_intersect_key($dateInfo, $keys);
	$defaults = array_fill_keys(array_keys($dateInfo), 0);   
	$parts = array_merge($defaults, $parts);
 
	return mktime($parts['seconds'], $parts['minutes'], $parts['hours'], $parts['mon'], $parts['mday'], $parts['year']);   
}

mkdate

mkdate.php
/**
 * 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);}

getWeekNr

getWeekNr.php
/**
 * date('W') liefert für uns die falsche Wochennummer. Wir wollen nach ISO 8601:1988
 * (erste Woche = Woche mit mindestens 4 Tage des neuen Jahres
 * @param timestamp $date
 * @return int
 */
function getWeekNr($date){
	//Windows kann nicht 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) );
}
php/functions/date/funktionen.txt · Last modified: 09.12.2013 09:39:54 (external edit)