Jump to content

Help creating function!


jmaccs64

Recommended Posts

Hello,

Can some please help me simplify this? Thanks!!!!

[code]//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";}[/code]
Link to comment
https://forums.phpfreaks.com/topic/18080-help-creating-function/
Share on other sites

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

[code]<?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);
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/18080-help-creating-function/#findComment-77514
Share on other sites

This is what i figured out...does anyone see any problem with this code? THANKS
[code]//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++;
}[/code]
Link to comment
https://forums.phpfreaks.com/topic/18080-help-creating-function/#findComment-77852
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.