silverbullet126 Posted October 18, 2006 Share Posted October 18, 2006 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 ? .. Quote Link to comment Share on other sites More sharing options...
obsidian Posted October 18, 2006 Share Posted October 18, 2006 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 Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted October 18, 2006 Share Posted October 18, 2006 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 Quote Link to comment Share on other sites More sharing options...
silverbullet126 Posted October 18, 2006 Author Share Posted October 18, 2006 Thanks obsidian and kenrbnsn. obsidian already have yours in testing .. should be up and working tomorrow ... man you saved me so much time :) thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.