Page is a not externally linkable
- Code, Content, and Presentation
-- PHP Server Side Scripting
---- Code challenge - how many minutes since the start of the week?


HelenDev - 2:26 pm on Jul 21, 2011 (gmt 0)


As requested, here is the code. The actual app is a bit more complex than this and split over multiple files but I have tried to strip out the complexities and specifics and put it in one piece, hopefully it still works and is of use to someone!

The idea of this code is that it powers both the admin end where you can see the calendar of links, and the front end where you just see today's scheduled link.


<?php

//set the beginning of the current cycle - originally 19 July 2010 but can reset
$cycle_start = mktime(0, 0, 0, 11, 01, 2010);

//get today's date
$todays_date = mktime(0, 0, 0, date("m"), date("d"), date("Y"));

//86400 seconds = 1 day
$seconds_per_day = 86400;

//show at least the next two weeks (14 days) in the calendar
$min_calendar_duration = 14;

//today is how long after cycle start?
$days_into_cycle = ($todays_date - $cycle_start)/$seconds_per_day;
//round days into cycle in case daylight saving time shafts our calculations by returning a fraction!
$days_into_cycle = round($days_into_cycle);

//get all the link ids and put them in an array
while($link_row = mysql_fetch_array($all_links_result)){
$links_array[]= $link_row["id"];
}

//count the number of links for each section
$number_links = count($links_array);

//are there more links to display than the min calendar duration?
$max_calendar_duration = max($number_links, $min_calendar_duration);

//get complete range of calendar
$calendar_range = $days_into_cycle + $max_calendar_duration;

//assign each link to a date, starting from the beginning of the cycle
$daycount = 1;
while($daycount <= $calendar_range){
foreach($links_array as $key => $link_id){

$link_day = $cycle_start + ($daycount * $seconds_per_day);
$links_dates_array[$link_day]= $link_id;
$daycount++;
}
}

//get today's links from the array
$link_today = $links_dates_array[$todays_date];

//start from today
$daycount = $days_into_cycle;

while($daycount < $calendar_range){

//calculate the day
$calendar_day = $cycle_start + ($daycount * $seconds_per_day);

//get this day's links from the array
$link_today = $links_dates_array[$calendar_day];

//get this day's links info from the database
$link_today_info = getLink($link_today);

//put this day's info into an array to put in the view table
$links_table_row[] = array (
'link_date' => date("l d F Y",$calendar_day),
'link_id' => $slink_today_info["id"],
'link' => substr($link_today_info["link"], 0, 100),
'link_order' => $link_today_info["ordering"];

$daycount++;
}
?>



[edit reason]Apologies for the delay and gazillions of owner edits, I could not get this code to post until I had gone through it and weeded out one problematic line![/edit]


Thread source:: http://www.webmasterworld.com/php/4336960.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com