PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sat Oct 20, 2018 9:05 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: Sun Jul 18, 2010 1:02 pm 
Offline
DevNet Master
User avatar

Joined: Mon Feb 24, 2003 11:12 am
Posts: 2572
Location: The Republic of Texas
Calculate the number of Friday the 13th occurrences from the first day in 1900 through the end of 2010.

_________________
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.


Top
 Profile  
 
PostPosted: Sun Jul 18, 2010 4:28 pm 
Offline
Forum Contributor
User avatar

Joined: Tue May 20, 2003 4:49 pm
Posts: 400
Syntax: [ Download ] [ Hide ]
<?php

$fri13 = 0;
for($year = 1900; $year <= 2010; $year++) {
        for($month = 1; $month <= 12; $month++) {
                if(date('l', mktime(0, 0, 0, $month, 13, $year)) == "Friday") {
                        //print date("F Y",  mktime(0, 0, 0, $month, 13, $year)) ." has a friday the 13th.\n";
                        $fri13++;
                }
        }
}
print"there are {$fri13} friday 13ths between 01-01-1900 and 31-12-2010.\n";
?>


Top
 Profile  
 
PostPosted: Sun Jul 18, 2010 5:12 pm 
Offline
DevNet Master
User avatar

Joined: Mon Feb 24, 2003 11:12 am
Posts: 2572
Location: The Republic of Texas
You're missing 4 8O

_________________
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.


Top
 Profile  
 
PostPosted: Sun Jul 18, 2010 5:40 pm 
Offline
Forum Contributor
User avatar

Joined: Tue May 20, 2003 4:49 pm
Posts: 400
bah no fair! mktime() only goes back to 1902.


Top
 Profile  
 
PostPosted: Sun Jul 18, 2010 6:44 pm 
Offline
Forum Contributor
User avatar

Joined: Tue May 20, 2003 4:49 pm
Posts: 400
Syntax: [ Download ] [ Hide ]
<?php
$fri13 = 0;

for($year = 1900; $year <= 2010; $year++) {
        for($month = 1; $month <= 12; $month++) {
                $date = date_create("{$year}-{$month}-13");
                if(date_format($date, "l") == "Friday") {
                        //print date_format($date, "Y m") ." has a friday the 13th.\n";
                        $fri13++;
                }
        }      
}
print"there are {$fri13} friday 13ths between 01-01-1900 and 31-12-2010.\n";
?>


fixed :)


Top
 Profile  
 
PostPosted: Tue Jul 20, 2010 2:45 am 
Offline
DevNet Master
User avatar

Joined: Wed Jun 27, 2007 9:44 am
Posts: 4313
Location: Sofia, Bulgaria
Syntax: [ Download ] [ Hide ]
class Friday13Calculator
{

        private static $fridays13inLeapYearWithWeekOffsets = array
        (
                1       => 2,
                2       => 1,
                3       => 2,
                4       => 2,
                5       => 1,
                6       => 1,
                7       => 3,
        );

        private static $fridays13inNonLeapYearWithWeekOffsets = array
        (
                1       => 2,
                2       => 2,
                3       => 1,
                4       => 3,
                5       => 1,
                6       => 1,
                7       => 2,
        );


        public static function get($startYear, $endYear)
        {
                $numberOfFridays13 = 0;
               
                for ($year = $startYear; $year <= $endYear; $year++)
                {
                        if (self::isLeapYear($year))
                                $fridays13InYear = self::$fridays13inLeapYearWithWeekOffsets;
                        else
                                $fridays13InYear = self::$fridays13inNonLeapYearWithWeekOffsets;

                        $numberOfFridays13 += $fridays13InYear[self::getWeekOffset($year)];
                }

                return $numberOfFridays13;
        }

        private static function isLeapYear($year)
        {
                if ($year % 4 != 0)
                        return false;
                if ($year % 100 != 0)
                        return true;
                if ($year % 400 != 0)
                        return false;
                return true;
        }


        private static function getWeekOffset($year)
        {
                return date_format(date_create('01-01-'.$year), 'N');
        }

}

echo Friday13Calculator::get(1900, 2010);

 


:)

_________________
There are 10 types of people in this world, those who understand binary and those who don't


Top
 Profile  
 
PostPosted: Tue Jul 20, 2010 1:25 pm 
Offline
DevNet Master
User avatar

Joined: Mon Feb 24, 2003 11:12 am
Posts: 2572
Location: The Republic of Texas
Nice work guys! Here's what I came up with:

Syntax: [ Download ] [ Hide ]
for($i=0, $start=new DateTime('1900-01-13'), $end=new DateTime('2010-12-31'); $start<=$end; $start->modify('13 of next month')) {
        if($start->format('N') == 5) {
                $i++;
                //$dates[] = $start->format('Y-m-d');  //if you need the dates
        }
}
echo $i;

_________________
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.


Last edited by AbraCadaver on Tue Jul 20, 2010 2:46 pm, edited 1 time in total.

Top
 Profile  
 
PostPosted: Tue Jul 20, 2010 2:36 pm 
Offline
DevNet Master
User avatar

Joined: Wed Jun 27, 2007 9:44 am
Posts: 4313
Location: Sofia, Bulgaria
Nice one :)

I've tried to speed up the process. I think the date_* functions are computational expensive.

I've been thinking about how to calculate the next weekOffset without using date_* functions - I just wanted to use the previous calculated value. It should be simple :)

_________________
There are 10 types of people in this world, those who understand binary and those who don't


Last edited by VladSun on Tue Jul 20, 2010 2:49 pm, edited 1 time in total.

Top
 Profile  
 
PostPosted: Tue Jul 20, 2010 2:43 pm 
Offline
DevNet Master
User avatar

Joined: Wed Jun 27, 2007 9:44 am
Posts: 4313
Location: Sofia, Bulgaria
Hm, I'm getting a warning with your code:

Quote:
Warning: DateTime::modify() [datetime.modify]: Failed to parse time string (13 of next month) at position 0 (1): Unexpected character ...


EDIT: PHP v5.3.2-1ubuntu4.2

_________________
There are 10 types of people in this world, those who understand binary and those who don't


Last edited by VladSun on Tue Jul 20, 2010 2:47 pm, edited 1 time in total.

Top
 Profile  
 
PostPosted: Tue Jul 20, 2010 2:45 pm 
Offline
DevNet Master
User avatar

Joined: Wed Jun 27, 2007 9:44 am
Posts: 4313
Location: Sofia, Bulgaria
Some speed benchmarks:
mine: 0.00524377822876 seconds
liljester: 0.0471768379211 seconds
AbraCadaver: Sorry, could not run it ...

_________________
There are 10 types of people in this world, those who understand binary and those who don't


Top
 Profile  
 
PostPosted: Tue Jul 20, 2010 2:50 pm 
Offline
DevNet Master
User avatar

Joined: Mon Feb 24, 2003 11:12 am
Posts: 2572
Location: The Republic of Texas
VladSun wrote:
Hm, I'm getting a warning with your code:

Quote:
Warning: DateTime::modify() [datetime.modify]: Failed to parse time string (13 of next month) at position 0 (1): Unexpected character ...


EDIT: PHP v5.3.2-1ubuntu4.2


That's strange, runs great for me. Seems like they broke something between 5.2.4 and 5.3.2 :(

PHP 5.2.4-2ubuntu5.10 with Suhosin-Patch 0.9.6.2 (cli)

_________________
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.


Top
 Profile  
 
PostPosted: Tue Jul 20, 2010 3:42 pm 
Offline
Moderator
User avatar

Joined: Mon Nov 03, 2003 7:13 pm
Posts: 5978
Location: Odessa, Ukraine
Syntax: [ Download ] [ Hide ]
echo 190;

Benchmark please :D


Last edited by Weirdan on Tue Jul 20, 2010 3:50 pm, edited 1 time in total.
Oops, used the wrong script for precalculation :)


Top
 Profile  
 
PostPosted: Tue Jul 20, 2010 3:46 pm 
Offline
DevNet Master
User avatar

Joined: Mon Feb 24, 2003 11:12 am
Posts: 2572
Location: The Republic of Texas
AbraCadaver wrote:
You're missing 4 8O


Same for you Weirdan :D

_________________
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.


Top
 Profile  
 
PostPosted: Tue Jul 20, 2010 3:52 pm 
Offline
Moderator
User avatar

Joined: Mon Nov 03, 2003 7:13 pm
Posts: 5978
Location: Odessa, Ukraine
AbraCadaver wrote:
Same for you Weirdan :D

Sorry, can't reproduce, please provide proper bugreport :P


Top
 Profile  
 
PostPosted: Tue Jul 20, 2010 4:01 pm 
Offline
DevNet Master
User avatar

Joined: Wed Jun 27, 2007 9:44 am
Posts: 4313
Location: Sofia, Bulgaria
2 and a half (Weirdan's code) people say it's 190 ... find your bug :P

_________________
There are 10 types of people in this world, those who understand binary and those who don't


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours


Who is online

Users browsing this forum: Google [Bot] and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group