Dates - Last Sunday of the month
Posted: Thu Jun 04, 2009 5:07 pm
Looking for a way to find if today is the last Sunday of the month.
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
$lastDay = date('t'); // number of days
$today = date('d');
$todayDay = date('D');
if ($todayDay != 'Sun' || ($lastDay - $today >=7) ) {
return false;
}
return true;
Code: Select all
$now = time();
if (date('l', $now) == 'Sunday' && date('n', $now) != date('n', strtotime('+1 week', $now))) {
echo 'This is the last Sunday of the month.';
}Code: Select all
function lastSunday(){
$now = time();
return(date('l', $now) == 'Sunday' && date('n', $now) == date('n', strtotime('+1 week', $now)));
}
Code: Select all
function getLastSunday() {
return date("Y-m-d", strtotime("last sunday", mktime(0,0,0,date("n")+1,1)));
}
function isTodayLastSunday() {
return date("Y-m-d") == getLastSunday();
}
Why?Darhazer wrote:I can't believe it!
* strtotime() is slowWeirdan wrote:Why?Darhazer wrote:I can't believe it!
You'd imagine it'd be dependant on locale, but who knowsDarhazer wrote:* Can you predict the behavior of strtotime()? If strtotime("01-02-03") is 01 Feb 2003, 02 Jan 2003, 02 Mar 2001, 03 Feb 2001 or what? (please answer without running it and without looking in php.net)
That depends on what you're trying to do. In some cases it could be quicker to call strtotime('next tuesday',$timestamp) than trying to figure that out with mathDarhazer wrote:* strtotime() is slow
I have no idea what you mean by this.Darhazer wrote:* strtotime() is the function, which implementation was changed most
PHP is written in English - developers need to learn some English to use it. Plus, if I'm an English speaker writing code for myself, why should I bother being language agnostic?Darhazer wrote:* every developer should be able to calculate date periods in programming manner, not using English expressions as '1 week ago'
That's what documentation is for - checking things you don't know. Do you know the order of arguments for strpos() without looking them up? in_array()? ltrim()? You shouldn't expect to have the whole language memorized.Darhazer wrote:* Can you predict the behavior of strtotime()? If strtotime("01-02-03") is 01 Feb 2003, 02 Jan 2003, 02 Mar 2001, 03 Feb 2001 or what? (please answer without running it and without looking in php.net)
Code: Select all
$now = time();
$one_hour_from_now = $now + 3600;Code: Select all
$now = time();
$one_hour_from_now = strtotime('+ 1 hour',$now);Quicker for writing, or for the interpreter to run it?That depends on what you're trying to do. In some cases it could be quicker to call strtotime('next tuesday',$timestamp) than trying to figure that out with math
Who cares? It's not a kind of code you would be running in a loop, so shaving off microseconds here won't buy you much performance overall. Get pragmatic, really.Darhazer wrote: * strtotime() is slow
I didn't know that. But would the output of above snippets be different in different php versions? I doubt it, though the burden of proof still lies on youDarhazer wrote: * strtotime() is the function, which implementation was changed most
1. Date manipulations are hard. Not every programmer is able to get them right. Most are unable, in fact. I've met many experienced programmers who were genuinely confused when they were given simple task of converting arbitrary UTC date/time to user-defined timezone. Many used current user's offset from UTC (as silly as it sounds, but they did).Darhazer wrote: * every developer should be able to calculate date periods in programming manner, not using English expressions as '1 week ago'
For the code posted here before you asked? Sure I can.Darhazer wrote: * Can you predict the behavior of strtotime()?
Years are four-digit currently, every developer should know that already (remember y2k problem?). And, given such string, how would you convert it to timestamp? If input is ambiguous the output is unpredictable unless you happen to know how the algorithm uses it - and that what documentation is for.Darhazer wrote: If strtotime("01-02-03")
Technically it doesn't try to parse the meaning out of any arbitrary English string. I think it only parses proper GNU date strings. There used to be a link to the document outlining what is allowed, but I can't find it any more.Darhazer wrote:this function tries to understand English
It was a link to a chapter of GNU Tar Manual, I believe. Though reading the changelog suggests the function was rewritten in 5.1.0 so I don't know if the information from tar manual still applies (but I guess it does).pickle wrote:There used to be a link to the document outlining what is allowed, but I can't find it any more.