the_toolman Posted September 13, 2019 Share Posted September 13, 2019 Hi there, I am trying to write a simple piece of code that will display a little text phrase on every Friday 13th in any year. How would I best go about doing this? Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/309224-if-the-day-is-a-friday-13th-then-display-text/ Share on other sites More sharing options...
Barand Posted September 13, 2019 Share Posted September 13, 2019 if (date('j') == 13 && date('w')== 5) { echo "You should have stayed in bed today" ; } See date() function Quote Link to comment https://forums.phpfreaks.com/topic/309224-if-the-day-is-a-friday-13th-then-display-text/#findComment-1569673 Share on other sites More sharing options...
requinix Posted September 13, 2019 Share Posted September 13, 2019 I'd go with 'l' and 'j'. And you could use them both at once to check for "Friday 13". Quote Link to comment https://forums.phpfreaks.com/topic/309224-if-the-day-is-a-friday-13th-then-display-text/#findComment-1569674 Share on other sites More sharing options...
the_toolman Posted September 13, 2019 Author Share Posted September 13, 2019 Hi, Thank you for the replies That worked perfectly! Quote Link to comment https://forums.phpfreaks.com/topic/309224-if-the-day-is-a-friday-13th-then-display-text/#findComment-1569675 Share on other sites More sharing options...
Barand Posted September 13, 2019 Share Posted September 13, 2019 19 minutes ago, requinix said: I'd go with 'l' and 'j'. I went for "w" as it's locale/language neutral. Quote Link to comment https://forums.phpfreaks.com/topic/309224-if-the-day-is-a-friday-13th-then-display-text/#findComment-1569677 Share on other sites More sharing options...
cyberRobot Posted September 16, 2019 Share Posted September 16, 2019 For what it's worth, you could also use "w" and "j" with the same date() call. if (date('w|j') == '5|13') { //if Friday the 13th echo "You should have stayed in bed today"; } The straight bar ("|") isn't necessary. Just using that to separate the values. Of course, that's not as intuitive as "Friday 13". Quote Link to comment https://forums.phpfreaks.com/topic/309224-if-the-day-is-a-friday-13th-then-display-text/#findComment-1569712 Share on other sites More sharing options...
Barand Posted September 16, 2019 Share Posted September 16, 2019 And other variations if ( date('l j') == 'Friday 13' ) if ( date('l d') == 'Friday 13' ) if ( date('D j') == 'Fri 13' ) if ( date('D d') == 'Fri 13' ) Quote Link to comment https://forums.phpfreaks.com/topic/309224-if-the-day-is-a-friday-13th-then-display-text/#findComment-1569714 Share on other sites More sharing options...
Psycho Posted September 17, 2019 Share Posted September 17, 2019 (edited) I didn't realize this was a challenge question. You're all being lazy relying on the date function 😁 function isFridayThirteenth($year, $month, $day) { $m = (($month+9)%12)+1; $C = floor($year/100); $Y = $year%100-(($m<11)?0:1); $W = ($day + floor(2.6*$m - 0.2) - (2*$C) + $Y + floor($Y/4) + floor($C/4)) % 7; return ($W==5 && $day==13); } Edited September 17, 2019 by Psycho 1 Quote Link to comment https://forums.phpfreaks.com/topic/309224-if-the-day-is-a-friday-13th-then-display-text/#findComment-1569768 Share on other sites More sharing options...
requinix Posted September 17, 2019 Share Posted September 17, 2019 5 minutes ago, Psycho said: I didn't realize this was a challenge question. It wasn't 😛 5 minutes ago, Psycho said: You're all being lazy relying on the date function 😁 Yours doesn't work properly for the year 2000. Quote Link to comment https://forums.phpfreaks.com/topic/309224-if-the-day-is-a-friday-13th-then-display-text/#findComment-1569769 Share on other sites More sharing options...
Psycho Posted September 17, 2019 Share Posted September 17, 2019 7 minutes ago, requinix said: Yours doesn't work properly for the year 2000. You're right. I was only considering future dates, but the same problem would occur in the year 2100. Apparently the problem stems from a flaw in the PHP logic for modulus which will return a negative number whereas most other languages only return a positive integer. I must fix this in case someone refers back to this post 80 years from now! I could define a custom modulus function to only return positive integers, but it's simpler for this process to just "hack" it by adding a hard-coded constant of 35 so the modulus of 7 will still be the same, but will never be negative. For those to whom it is not obvious, this is all sarcasm. I did this as a mental exercise and am not proposing that this is a preferred solution in any way. But, it does work function isFridayThirteenth($year, $month, $day) { $k = $day; $m = (($month+9)%12)+1; $C = floor($year/100); $Y = $year%100-(($m<11)?0:1); $W = ($k + floor(2.6*$m - 0.2) - (2*$C) + $Y + floor($Y/4) + floor($C/4) + 35) % 7; return ($W==5 && $day=13); } Quote Link to comment https://forums.phpfreaks.com/topic/309224-if-the-day-is-a-friday-13th-then-display-text/#findComment-1569776 Share on other sites More sharing options...
Barand Posted September 17, 2019 Share Posted September 17, 2019 I fails for a few historic dates $dt1 = new DateTime('2019-09-13'); $di = new DateInterval('P1M'); while ($dt1->format('Y') >= 1901) { if ($dt1->format('D j') == 'Fri 13') { if ( !isFridayThirteenth($dt1->format('Y'), $dt1->format('n'), $dt1->format('j') ) ) { echo $dt1->format('Y-m-d') . ' - Failed<br>'; } } $dt1->sub($di); } /* RESULTS : 2015-03-13 - Failed 2012-04-13 - Failed 2011-05-13 - Failed 2009-03-13 - Failed 2008-06-13 - Failed 2007-07-13 - Failed 2007-04-13 - Failed 2005-05-13 - Failed 2004-08-13 - Failed 2003-06-13 - Failed 2002-09-13 - Failed 2001-07-13 - Failed 2001-04-13 - Failed 2000-10-13 - Failed 1914-03-13 - Failed 1910-05-13 - Failed 1908-03-13 - Failed 1906-07-13 - Failed 1906-04-13 - Failed 1904-05-13 - Failed 1903-03-13 - Failed 1902-06-13 - Failed 1901-09-13 - Failed */ EDIT: P.S. Confirmed you're good until August 2100 2100-08-13 - Failed 2101-05-13 - Failed 2102-10-13 - Failed 2103-04-13 - Failed 2103-07-13 - Failed 2104-06-13 - Failed 2105-03-13 - Failed 2106-08-13 - Failed 2107-05-13 - Failed 2108-04-13 - Failed 2108-07-13 - Failed 2110-06-13 - Failed 2111-03-13 - Failed 2112-05-13 - Failed 2114-04-13 - Failed 2116-03-13 - Failed 2200-06-13 - Failed Quote Link to comment https://forums.phpfreaks.com/topic/309224-if-the-day-is-a-friday-13th-then-display-text/#findComment-1569777 Share on other sites More sharing options...
Psycho Posted September 17, 2019 Share Posted September 17, 2019 1 hour ago, Barand said: I fails for a few historic dates EDIT: P.S. Confirmed you're good until August 2100 I guess you missed the update I posted in response to @requinix It now works for those previous dates as well as the years that end in "00". Tested from year 1 to 3000. I think that should be sufficient. I was really just interested in implementing a calculation to find the day of the week. I think I'll stick with using date() from now on. Quote Link to comment https://forums.phpfreaks.com/topic/309224-if-the-day-is-a-friday-13th-then-display-text/#findComment-1569786 Share on other sites More sharing options...
requinix Posted September 17, 2019 Share Posted September 17, 2019 15 minutes ago, Psycho said: I think I'll stick with using date() from now on. Traitor. Quote Link to comment https://forums.phpfreaks.com/topic/309224-if-the-day-is-a-friday-13th-then-display-text/#findComment-1569788 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.