Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

PHP:Calendar

Contributor ,
Apr 10, 2011 Apr 10, 2011

I've been trying to build a calendar using PHP.

Im using getdate and cal_days_in_month(CAL_GREGORIAN, $month, $year); to get the info I need to build a calendar.

What I'm trying to do  use the info from the getdate array to give me the months start date, and how may days in that month (got that info).

place the first date (1) in the row of a table that refelcts the day (sun=0) that starts at that position and will loop the cal_days_in_month, placing the other dates

in increasing order as it loops.

I'm having problem figuring out how to get PHP to find /get an html id tag (and then place that number into it).

If my logic is wrong, or someone has a better way, any help is appreciate

TOPICS
Server side applications
621
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 22, 2011 Apr 22, 2011

this help?

if (isset($_GET['month'])) // If we receive month argument
    {
        if (isset($_GET['year'])) // If we receive year argument
        {
            $currentMonth = $_GET['month'];
            $currentYear = $_GET['year'];
            if ($_GET['year'] <= date('Y'))
            {
                if ($_GET['month'] < date('n') || $_GET['year'] < date('Y'))
                {
                    $pastmonth = 1;
                }
            }
        }
        else
        {
            $currentMonth = $_GET['month'];
            $currentYear = date('Y');
            if ($_GET['month'] < date('n'))
            {
                $pastmonth = 1;
            }
        }
    }
    else // No month argument received
    {
        $currentMonth = date('n'); //10
        $currentDay = date('d');
        $currentYear = date('Y'); // 2010
    }
   
    if (isset($_GET['month']) || isset($_GET['year']))
    {
        if (($_GET['month'] == date('n')) && (($_GET['year'] == date('Y')) || !$_GET['year'][0])) // If this is the current month
        {
            $currentDay = date('d');
        }
    }
    else
    {
        $currentDay = 1;
    }
   
    $currentdow = date('w'); // 0-6 weekday
   
    $fdom = firstDayMonth($currentMonth, $currentYear);
    $weeksInMonth = weeksInMonth($currentMonth, $currentYear);
    $daysInMonth = daysInMonth($currentMonth, $currentYear);
    $daycount = '1';
    $date = array();

<?php for ($i=0; $i <= 6; $i++) { ?>
       <td>
        <center><h3><?php echo $dow[$i]; ?></h3></center>
       </td>
      <?php } ?>
     </thead>
    <!--- Weeks --->
    <?php for ($w=0; $w < $weeksInMonth; $w++) { /* each week */ ?>
     <tr>
      <?php for ($i=0; $i <= 6; $i++) /* each day */
       {
         $dayClass = 'weekday';
         if ($i >= 5)
         {
            $dayClass = 'weekend';
         }
         if ( (($w=='0' && ($fdom <= $i)) || ($w > 0)) && $i <= 6)
         {
            if (($daycount == $currentDay) && ($currentMonth == date('n')) && ($currentYear == date('Y'))) // today
            {
                ?>
                <td><div class="table-today"><b><div class="ped"><?php echo $daycount ?></b><br />
                <center>
                <span class="startfinish">Today</span>
                </center>
                <?php
            }
            elseif (($currentDay <= $daycount) && ($daycount <= $daysInMonth) && ($pastmonth == 0))
            {
                ?>
                <td><div class="table-<?php echo $dayClass ?>"><div class="ped"><?php echo $daycount ?><br />
                <center>
                <span class="startfinish">
                <?php echo availabilityCheck($daycount, $recalled) ?>
                </span>
                </center>
                <?php
            }
            elseif ($i <= 6 && ($daycount > $daysInMonth)) // tds after end of month
            {
                ?>
                <td><div class="table-notInMonth"><div class="ped">
                <?php
            }
            else
            {
                ?>
                <td><div class="table-<?php echo $dayClass ?>"><div class="ped"><?php echo $daycount; ?>
                <?php
            }
            $daycount++;
          }
          else // tds in begining of month
          {
            ?><td><div  class="table-notInMonth"><div class="ped"><?php
          } ?>
       </div></div>
      </td><?php
      } ?>
     </tr><?php
     } ?>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Apr 22, 2011 Apr 22, 2011

Your code sample looks inteesting but makes calls to three functions that are not included. firstDayMonth, weeksInMonth, and daysInMonth.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 22, 2011 Apr 22, 2011
LATEST

$dow = array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');

function firstDayMonth($month, $year) // What day of the week is the first of the month
{
    $dayone = mktime(0,0,0, $month, 1, $year);
    $dayow = date('w', $dayone);
    return $dayow;
}

function daysInMonth($month, $year)
{
    $datetime = mktime(0, 0, 0, $month, 1, $year);
    $daysim = date('t', $datetime);
    return $daysim;
}

function weeksInMonth($month, $year) // how many weeks are in a month
{
    $date['fdom'] = firstDayMonth($month, $year);
    $date['example'] = mktime(0, 0, 0, $month, 1, $year);
    $date['dim'] = daysInMonth($month, $year);
    $date['wim'] = 0;
    while ($date['dim'] > 0)
    {
        if ($date['fdom'] != 0)
        {
            $date['dim'] = $date['dim'] - (7 - $date['fdom']);
            $date['fdom'] = 0;
            $date['wim']++;
        }
        elseif ($date['dim'] > 6)
        {
            $date['dim'] = $date['dim'] - 7;
            $date['wim']++;
        }
        else
        {
            $date['dim'] = 0;
            $date['wim']++;
        }
    }
    return $date['wim'];
}

function getDaysInWeek($weekNumber, $year)
{
  // Count from '0104' because January 4th is always in week 1
  // (according to ISO 8601).
  $time = strtotime($year . '0104 +' . ($weekNumber - 1)
                    . ' weeks');
  // Get the time of the first day of the week
  $mondayTime = strtotime('-' . (date('w', $time) - 1) . ' days',
                          $time);
  // Get the times of days 0 -> 6
  $dayTimes = array ();
  for ($i = 0; $i < 7; ++$i) {
    $dayTimes[] = strtotime('+' . $i . ' days', $mondayTime);
  }
  // Return timestamps for mon-sun.
  return $dayTimes;
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines