Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

R's Simple Smarty Calendar

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks
View previous topic :: View next topic  
Author Message
RGordijn
Smarty Rookie


Joined: 21 Jan 2008
Posts: 15

PostPosted: Fri Feb 08, 2008 10:49 am    Post subject: R's Simple Smarty Calendar Reply with quote

Hi,

My mission for today was to create a simple calendar using smarty.
I picked some examples from the web, and this is the result:


the .php file
Code:
<?php
// smarty loading etc...

$now = time();

if ( !empty ( $_GET['y'] ) ) {
   $year = $_GET['y'];
} else {
   $year = date('Y',$now);
}

if ( !empty ( $_GET['m'] ) ) {
   $month = $_GET['m'];
} else {
   $month = date('m',$now);
}

/**
 * want to start on sunday? use this array AND ( important! ) set $day_offset to 0 ( zero )
 * $days = array('sunday','monday','tuesday','wednesday','thursday','friday','saturday');
 */
$days = array('monday','tuesday','wednesday','thursday','friday','saturday','sunday');
$months = array('','january','febuary','march','april','may','june','july','august','september','october','november','december');

// day offset, 1 is monday, 0 is sunday
$day_offset = 1;

$start_day = gmmktime ( 0, 0, 0, $month, 1, $year );
$start_day_number = date ( 'w', $start_day );
$days_in_month = date ( 't', $start_day );
$row = 0;
$cal = array();
$trow = 0;
$blank_days = $start_day_number - $day_offset;

if ( $blank_days < 0 ) {
   $blank_days = 7 - abs ( $blank_days );
}

for ( $x = 0 ; $x < $blank_days ; $x++ ) {
   $cal[ $row ][ $trow ]['num'] = null;
   $trow++;
}

for ( $x = 1 ; $x <= $days_in_month ; $x++ ) {
   
   if ( ( $x + $blank_days - 1 ) % 7 == 0 ) {
      $row++;
   }
   $cal[ $row ][ $trow ]['num'] = $x;
   $cal[ $row ][ $trow ]['ts'] = mktime ( 0, 0, 0, $month, $x, $year );
   $trow++;
}
while ( ( ( $days_in_month + $blank_days ) % 7 ) != 0 ) {
   $cal[ $row ][ $trow ]['num'] = null;
   $days_in_month++;
   $trow++;
}

$main->tpl->assign('months',$months);
$main->tpl->assign('days',$days);
$main->tpl->assign('cal',$cal);
$main->tpl->assign('month',abs($month));
$main->tpl->assign('year',$year);

// smarty display etc
?>



the .tpl file
Code:
<table border="0" cellpadding="2" cellspacing="2" align="center">
<tr>
   <td colspan="7" align="center">
      {if $month == 1}
         {assign var=t_month value=12}
         {math equation='( y - 1 )' y=$year assign=t_year}
      {else}
         {math equation='( m - 1 )' m=$month assign=t_month}
         {assign var=t_year value=$year}
      {/if}
      
      {if $month == 12}
         {assign var=v_month value=1}
         {math equation='( y + 1 )' y=$year assign=v_year}
      {else}
         {math equation='( m + 1 )' m=$month assign=v_month}
         {assign var=v_year value=$year}
      {/if}
      
      <a href="{$smarty.server.SCRIPT_NAME}?y={$t_year}&m={$t_month}"><<-</a>
      <b>{$months[$month]} : {$year}</b>
      <a href="{$smarty.server.SCRIPT_NAME}?y={$v_year}&m={$v_month}">->></a>
   </td>
</tr>
<tr>
{foreach from=$days item=i}
   <td align="center">
      <b>{$i}</b>
   </td>
{/foreach}
</tr>

{foreach from=$cal item=i}
<tr>
   {foreach from=$i item=o}
      <td style="border:1px dotted black;" width="80" height="80" valign="middle" align="center">
      {if $o.num == null}
         &nbsp;
      {else}
         {$o.num}
         {* you can also use $o.ts for a unix timestamp of this day *}
      {/if}
      </td>
   {/foreach}
</tr>
{/foreach}

</table>


Maybe someone can use it for something.

Comments are welcome!

cheers

Rob


Last edited by RGordijn on Sat Aug 03, 2019 4:28 pm; edited 1 time in total
Back to top
View user's profile Send private message
webharan
Smarty Rookie


Joined: 08 Jan 2008
Posts: 5

PostPosted: Fri Mar 07, 2008 3:56 am    Post subject: hi Reply with quote

Checked through the code...There seems to be a mismatch in the way months are displayed.

Need to check the logic. Rolling Eyes
Back to top
View user's profile Send private message
kkruger
Smarty n00b


Joined: 25 Sep 2011
Posts: 1

PostPosted: Sun Sep 25, 2011 10:00 am    Post subject: this works but,... Reply with quote

the mismatch is caused by the way gmmktime looks back at you with the given time(), depending on your time zone. Use mktime instead.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP