Jump to content


Photo

Case of the mondays


  • Please log in to reply
3 replies to this topic

#1 silverbullet126

silverbullet126
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 18 October 2006 - 05:27 PM

Hey all,

Our website has chart (top 20 kinda thing) and its updated every Monday, I don't have any problem with getting the database working and having the chart up to update on the Mondays etc. But the boss wants to see past charts posted, this isn't a big deal, but doing it dynamically could be a problem. Here is what i have going on now:

$sql_week=date ('W');

if ($sql_week == 42) {
$weekof="week of October 16th";
$show_week="42";
}

I have one of these for each week, 42 to 45, but i now need to go back to week 32 and that’s a lot of typing. I was wondering if someone could help me out and make a script based on the week of the year that would bring up the date of each Monday (base on that week of the year) .. like $show_week="42" and $monday_date="16" ... and this would be dynamic based on $sql_week. 

Anyone ? ..


#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 18 October 2006 - 06:52 PM

here's what i came up with. may not be the prettiest, but it gives you all your possible mondays for the year up to this point:
<?php
$day  = 60 * 60 * 24;
$week = $day * 7;
$mondays = array();
$date = mktime(0,0,0,1,1,date('Y'));
while (date('w', $date) != 1) {
  $date += $day;
}

$myWeeks = array();
for ($i = 1; $i <= date('W'); $i++) {
  list($y,$m,$d) = explode('-', date('Y-m-d', $date));
  $myWeeks[$i] = array('month' => $m, 'day' => $d);
  $date += $week;
}
?>

then, you can simply use the week you're after as the key to the array and pull the month or day like this:
<?php
// get the monday for the 16th week of the year
$month = $myWeeks[16]['month'];
$day   = $myWeeks[16]['day'];
?>

hope this helps
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 18 October 2006 - 08:24 PM

I came up with something similar:
<?php
$this_year = date('Y');
$next_year = $this_year + 1;
$first_monday = strtotime('1st monday',strtotime('1/1/' . $this_year));
$tst = $first_monday;
$weeks = array();
while(date('Y',$tst) < $next_year) {
    $weeks[date('W',$tst)] = array($tst,date('Y-m-d',$tst)); // store date in an array both as a timestamp and as ascii string
    echo date('Y-m-d --> W',$tst)."\n";
    $tst += (86400*7);
}
print_r($weeks);  // show the array
?>

Ken

#4 silverbullet126

silverbullet126
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 18 October 2006 - 08:29 PM

Thanks obsidian and kenrbnsn.

obsidian already have yours in testing .. should be up and working tomorrow ... man you saved me so much time :)

thanks




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users