# Help creating function!

4 replies to this topic

### #1 jmaccs64

jmaccs64
• Members
• 34 posts

Posted 20 August 2006 - 06:32 AM

Hello,

```//This will find current week
\$today = date("YmdHis");
\$week_array = array(20060819230300, 20060819231400, 20060924100000);
if (\$today<\$week_array[0])
{\$week = "1";}
elseif (\$today<\$week_array[1])
{\$week = "2";}
elseif (\$today<\$week_array[2])
{\$week = "3";}
else {\$week= "0";}```

### #2 GingerRobot

GingerRobot
• Staff Alumni
• 4,086 posts
• LocationUK

Posted 20 August 2006 - 08:39 AM

What is it supposed to do?

### #3 Barand

Barand
• Moderators
• Sen . ( ile || sei )
• 18,021 posts

Posted 20 August 2006 - 10:08 AM

Quite!

You would expect an array of dates each 7 days apart, but that has 2 dates same (19th) then the 24th ??? ??? ???

Here's a couple of options

```<?php
// METHOD 1 - assumes weeks start on a Monday
//
//  if first day of month is in week 31 of the year
//  and today is in 33rd week
//  then we're in 3rd week of the month
//
//
\$weekfirst = date('W', mktime(0,0,0,date('m'),1,date('Y')));
\$week = date('W')-\$weekfirst+1;

// METHOD 2 - assumes weeks start on first day of current month
//
//  days 1-7 of month in first week
//  8 - 14 in 2nd week
//  15 - 21  in 3rd week
//  22 - 28 in 4th week
//
\$week = 1 + floor((date('j') - 1)/7);
?>```

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

 |baaGrid| easy data tables - and more|baaChart| easy line, column and pie charts

### #4 jmaccs64

jmaccs64
• Members
• 34 posts

Posted 20 August 2006 - 02:31 PM

This will have about 19 dates in it. These dates are for the start date and time of each week of the NFL season, each week the first game time could be different, hence why i can't use the week function of date() Thanks! Any suggestions?

### #5 jmaccs64

jmaccs64
• Members
• 34 posts

Posted 21 August 2006 - 02:49 AM

This is what i figured out...does anyone see any problem with this code? THANKS
```//This will find current week
\$today = date("YmdHis");
\$week_array = array(1 => 20060907173000, 20060917100000, 20060924100000, 20061001100000, 20061008100000, 20061015100000, 20061022100000, 20061029100000, 20061105100000, 20061112100000, 20061119100000, 20061123093000, 20061130170000, 20061207170000, 20061214170000, 20061221170000, 20061230170000);
\$week_array_count = count(\$week_array);
\$i = 1;
while((\$i <= \$week_array_count) && (!\$week)){
if(\$today<\$week_array[\$i]){
\$week="\$i";

};
\$i++;
}```

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users