Jump to content

Case of the mondays


silverbullet126

Recommended Posts

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 ? ..
Link to comment
Share on other sites

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:
[code]
<?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;
}
?>
[/code]

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

hope this helps
Link to comment
Share on other sites

I came up with something similar:
[code]<?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
?>[/code]

Ken
Link to comment
Share on other sites

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.