note 28121 modified in ref.datetime by danbrown

From: Date: Sun, 20 Jul 2008 02:27:35 +0000
Subject: note 28121 modified in ref.datetime by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-141961@lists.php.net to get a copy of this message
I needed a function that determined the last Sunday of the month. Since it's made for the website's "next meeting" announcement, it goes based on the system clock; also, if today is between Sunday and the end of the month, it figures out the last Sunday of *next* month. lastsunday() takes no arguments and returns the date as a string in the form "January 26, 2003". I could probably have streamlined this quite a bit, but at least it's transparent code. =) <?php /* The two functions calculate when the next meeting will be, based on the assumption that the meeting will be on the last Sunday of the month. */ function getlast($mon, $year) { $daysinmonth = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); $days = $daysinmonth[$mon-1]; if ($mon == 2 && ($year % 4) == 0 && (($year % 100) != 0 || ($year % 400) == 0)) $days++; if ($mon == 2 && ($year % 4) == 0 && ($year % 1000) != 0) $days++; $lastday = getdate(mktime(0,0,0,$mon,$days,$year)); $wday = $lastday['wday']; return getdate(mktime(0,0,0,$mon,$days-$wday,$year)); } function lastsunday() { $today = getdate(); $mon = $today['mon']; $year = $today['year']; $mday = $today['mday']; $lastsun = getlast($mon, $year); $sunday = $lastsun['mday']; if ($sunday < $mday) { $mon++; if ($mon = 13) { $mon = 1; $year++; } $lastsun = getlast($mon, $year); $sunday = $lastsun['mday']; } $nextmeeting = getdate(mktime(0,0,0,$mon,$sunday,$year)); $month = $nextmeeting['month']; $mday = $nextmeeting['mday']; $year = $nextmeeting['year']; return "$month $mday, $year"; } ?> --was-- I needed a function that determined the last Sunday of the month. Since it's made for the website's "next meeting" announcement, it goes based on the system clock; also, if today is between Sunday and the end of the month, it figures out the last Sunday of *next* month. lastsunday() takes no arguments and returns the date as a string in the form "January 26, 2003". I could probably have streamlined this quite a bit, but at least it's transparent code. =) /* The two functions calculate when the next meeting will be, based on the assumption that the meeting will be on the last Sunday of the month. */ function getlast($mon, $year) { $daysinmonth = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); $days = $daysinmonth[$mon-1]; if ($mon == 2 && ($year % 4) == 0 && (($year % 100) != 0 || ($year % 400) == 0)) $days++; if ($mon == 2 && ($year % 4) == 0 && ($year % 1000) != 0) $days++; $lastday = getdate(mktime(0,0,0,$mon,$days,$year)); $wday = $lastday['wday']; return getdate(mktime(0,0,0,$mon,$days-$wday,$year)); } function lastsunday() { $today = getdate(); $mon = $today['mon']; $year = $today['year']; $mday = $today['mday']; $lastsun = getlast($mon, $year); $sunday = $lastsun['mday']; if ($sunday < $mday) { $mon++; if ($mon = 13) { $mon = 1; $year++; } $lastsun = getlast($mon, $year); $sunday = $lastsun['mday']; } $nextmeeting = getdate(mktime(0,0,0,$mon,$sunday,$year)); $month = $nextmeeting['month']; $mday = $nextmeeting['mday']; $year = $nextmeeting['year']; return "$month $mday, $year"; } http://php.net/manual/en/ref.datetime.php

« previous php.notes (#141961) next »