Skip to content

Instantly share code, notes, and snippets.

@pmgupte
Last active October 6, 2016 07:03
Show Gist options
  • Save pmgupte/5796388 to your computer and use it in GitHub Desktop.
Save pmgupte/5796388 to your computer and use it in GitHub Desktop.
PHP function to count how many times given day is occurring in then given month, on given date.
<?php
/**
* Copyright (C) 2016 Prabhas Gupte
*
* This is free script: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should also see <http://www.gnu.org/licenses/gpl.txt>
*/
function count_occurances($day, $date, $month = 'this month') {
$first_occurance = date("d", strtotime("first $day of $month"));
$count = ceil(($date - $first_occurance)/7 + 1);
return $count;
}
$day = date("D");
$date_today = date("d");
echo "\nMonday, 20th May: ", count_occurances("Mon", 20);
echo "\n$day, $date_today: ", count_occurances($day, $date_today);
echo "\nMonday, 10th June: ", count_occurances("Mon", 10, 'June 2013');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment