Re: Last Sunday in September?
| From: | tg-php at gryffyndevelopment dot com | Date: | Fri, 17 Feb 2006 18:23:14 +0000 |
| Subject: | Re: Last Sunday in September? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-230656@lists.php.net to get a copy of this message | ||
There's going to be 'tighter' and more efficient ways to do this.. probably some
using strtotime(). I have an irrational distrust of the results from strtotime() though, so I use
it sparingly.
Here's a simple solution though:
<?php
// Designate month
$month = "2";
// Designate year
$year = "2006";
// Find last day of the month by finding how many days are in month
// Could also do $month + 1, $day = 0
$lastday = date("t", mktime(0, 0, 0, $month, 1, $year));
// Find out what numerical day of the week it is, 0 = sunday, 6 = saturday
$dayofweek = date("w", mktime(0, 0, 0, $month, $lastday, $year));
// Subtract day of week from current day. If you're already on last
// sunday of the month, it'll be $lastday - 0 so it all works out
$lastsunday = date("Y-m-d", mktime(0, 0, 0, $month, $lastday - $dayofweek, $year));
echo $lastsunday;
?>
There's going to be a dozen ways to skin this one, just showing one way.
-TG
= = = Original message = = =
Hi,
I'm building a program and I need to find the last Sunday in September for
every year because the following Monday is the start of a new year for us.
So 2006 ends on September 24th 2006 and the new year (2007) starts on
September 25th 2006. I was thinking that using the strtotime() would get me
this information possibly? Is there an easy way to get this information?
Pseudo code:
If ((date > last Sunday in September this year) &&
(date < Jan 1 of next year))
year++
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
___________________________________________________________
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.