CodeIgniter Forums

Full Version: Generating week
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]sojic[/eluser]
I need current week navigation!

Example:

Code:
<p>
<a href="2008-12-01">Mon</a>
<a href="2008-12-02">Tue</a>
<a href="2008-12-03">Wed</a>
<a href="2008-12-04">Thu</a>
<a href="2008-12-05">Fri</a>
<a href="2008-12-06">Sat</a>
<a href="2008-12-07">Sun</a>
</p>

El Forum

[eluser]Bogdan Tanase[/eluser]
.. and I need a beer! Tongue

Lookup Calendaring Class. It might have something usefull. If not, PHP date/time functions...

El Forum

[eluser]xwero[/eluser]
The calendar class only creates a month calendar view.

To get a weeks worth of dates you can get the week number of the day, date('w'). And based on that you set the date for sunday (0) and saturday (6). And then its a matter of looping from sunday until saturday.

El Forum

[eluser]Spir[/eluser]
It would be nice if people would share some code for that matter Smile

El Forum

[eluser]xwero[/eluser]
This function accepts a unix timestamp an returns an array of date based on the weekday numbering used by the date function, meaning 0 as sunday and 6 as saturday.

The function can be the base of a week view.
Code:
function get_week($date)
{
   $week_days = range(0,6);
   $week_day = date('w',$date);

   foreach($week_days as $key=>$day)
   {
      if($day == $week_day)
      {
          $week_days[$key] = $date;
      }
      elseif($day < $week_day)
      {
         $week_days[$key] = strtotime('-'.$week_day+$day.' day',$date);
      }
      elseif($day > $week_day)
      {
         $week_days[$key] = strtotime('+'.$day-$week_day.' day',$date);
      }
   }
  
   return $week_days;
}

El Forum

[eluser]Spir[/eluser]
Nice. I have tried this :

Code:
// July 1, 2000
$arr = get_week(date(mktime(0, 0, 0, 7, 1, 2000)));

for ($i=0;$i<count($arr);$i++){
    echo date('d m Y', $arr[$i]).'<br />';
}

Which output this :
Quote:25 06 2000
26 06 2000
27 06 2000
28 06 2000
29 06 2000
30 06 2000
01 07 2000

So now if I want my week to start with monday I have to change the range right?

Code:
$week_days = range(1,7);

El Forum

[eluser]Spir[/eluser]
Ok so I have build something using your function, but I have a small issue. Let me show you what I have :

In my controller :

Code:
class week extends Controller {

    function index($year='', $month='', $day=''){
        if ($year==null) {
            $year = date('Y');
        }
        
        if ($month==null) {
            $month = date('m');
        }
        
        if ($day==null) {
            $day = date('d');        
        }    

        $calendarPreference = array (
                        'start_day'    => 'monday',
                        'month_type'   => 'long',
                        'day_type'     => 'long',
                        'date'     => date(mktime(0, 0, 0, $month, $day, $year))
                    );        
        $this->load->library('calendar_week', $calendarPreference);

        // I need to feed my calndar week with some data
        // for the example data are empty
        $weeks = $this->calendar_week->get_week();
        $arr_Data = Array();
        for ($i=0;$i<count($weeks);$i++){
            $arr_Data[$weeks[$i]] = '';
        }

        $content['data'] = $arr_Data;      
        $this->load->view('show_week', $content);
        ...
    }
}

In my view I only have this :
Code:
&lt;?php echo $this->calendar_week->generate($data); ?&gt;

And here is my lib called calendar_week (it use the language file of the Calendar) : http://rafb.net/p/HzRvNf45.html
It works fine. The only issue is when I press previous week, I shows the same week.I think the issue comes from your function ''set_week()'' that does not set the propper week because I send the sunday of the previous week.

El Forum

[eluser]Spir[/eluser]
I have temporary fixed the bug by setting :
$before = strtotime('-4 day',$this->week_days[0]);

In calendar lib.

El Forum

[eluser]darky84[/eluser]
Dear Spir

the link you posted for downloading your lib calendar_week is not working, could you please resubmit the link or share your code, i need this week view please

thanks

El Forum

[eluser]Spir[/eluser]
[quote author="darky84" date="1366808947"]Dear Spir

the link you posted for downloading your lib calendar_week is not working, could you please resubmit the link or share your code, i need this week view please

thanks [/quote]You are damn lucky I still look at ellisLab mail and had this lib somewhere in my backups! This is a 4 years old topic/lib.
Here you go: http://pastebin.com/7NHwZENK
Pages: 1 2