Skip to content

Instantly share code, notes, and snippets.

@aszel
Created July 23, 2014 12:50
Show Gist options
  • Save aszel/37ade3b9746637273da2 to your computer and use it in GitHub Desktop.
Save aszel/37ade3b9746637273da2 to your computer and use it in GitHub Desktop.
<?php
// http://php.net/manual/de/function.date.php
// Zeilenumbruch
$b = "\n";
$bb = "\n\n";
$bbb = "\n\n\n";
#################################################### Umgang mit date()
#echo date("l") . $b;
#echo date('l jS \of F Y h:i:s A') . $b;
#echo "Weekday: " . date('l') . $b;
#echo "Weekday as number:" . date('w') .$b;
#echo "Day of month as number: " . date('d') . $b;
#echo "Month: " . date('F') . $bbb;
#echo "July 27, 1981 war ein " . date("l", mktime(0, 0, 0, 7, 27, 1981)) . " und der " . date('w', mktime(0, 0, 0, 7, 27, 1981)) . "te Tag der Woche." . $b;
###################################################
/*
* Returns the difference of months between current month and given date
* @param $date a YYYY-MM-DD formatted date
*/
function getDiffBetweenCurMonthAndTravelMonth($date)
{
// current date
$curDate = date('Y') . "-". date('m'). "-" . date('d');
$ts1 = strtotime($curDate);
$ts2 = strtotime($date);
$year1 = date('Y', $ts1);
$year2 = date('Y', $ts2);
$month1 = date('m', $ts1);
$month2 = date('m', $ts2);
$diff = (($year2 - $year1) * 12) + ($month2 - $month1);
return $diff;
}
#echo "diff month: " . getDiffBetweenCurMonthAndTravelMonth("2014-08-02") . "\n";
/**
* Returns number of weekday
* 1 = Montag ... 7 = Sonntag
* @param $date a YYYY-MM-DD formatted date
*/
function getDayNumberOfWeek($date)
{
$ts = strtotime($date);
$year = date('Y', $ts);
$month = date('m', $ts);
$day = date('d', $ts);
$numDay = date("w", mktime(0, 0, 0, $month, $day, $year));
if ($numDay == 0) {
return 7;
}
return $numDay;
}
echo "weekday number of 27.07.1981: " . getDayNumberOfWeek("1981-07-27") . "\n";
/**
* Returns the amount of weeks into the month a date is
* @param $date a YYYY-MM-DD formatted date
* @param $rollover The day on which the week rolls over
*/
function getWeekOfMonth($date, $rollover)
{
$cut = substr($date, 0, 8);
$daylen = 86400;
$timestamp = strtotime($date);
$first = strtotime($cut . "00");
$elapsed = ($timestamp - $first) / $daylen;
$i = 1;
$weeks = 1;
for($i; $i<=$elapsed; $i++)
{
$dayfind = $cut . (strlen($i) < 2 ? '0' . $i : $i);
$daytimestamp = strtotime($dayfind);
$day = strtolower(date("l", $daytimestamp));
if($day == strtolower($rollover)) $weeks ++;
}
return $weeks;
}
//outputs 2, for the second week of the month
#echo getWeekOfMonth("2014-07-23", "monday") . "\n"; // 4
#echo getWeekOfMonth("2014-02-02", "monday") . "\n"; // 1
#echo getWeekOfMonth("2014-02-03", "monday") . "\n"; // 2
#echo getWeekOfMonth("2011-08-01", "monday") . "\n"; // 1
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment