Jump to content

Recommended Posts

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".

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 by Psycho
  • Like 1
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.

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);
}

 

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

 

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.