0

I have a calendar set up in a mySQL table and I am trying to output the dates via monthly calendars (Jan, Feb, Mar) which accounts for the "missing days" for previous months and the following month.

So if I am trying to make a calendar for Jan 2013 and Jan 1 falls on a Tuesday -- I am having difficulty coding a method to show that Sun Dec 30 and Mon Dec 31 act as "space fillers" or what have you... know what I mean?

I am having a difficult time wrapping my head around how to output this info.

The data structure and data is as follows:

Table_name: sched_calendar_table

dt  y   q   m   d   dw  monthName   dayName w   isWeekday   isHoliday   holidayDescr    isPayday
2010-01-01  2010    1   1   1   6   January Friday  0   1   1   New Year's Day  0
2010-01-02  2010    1   1   2   7   January Saturday    0   0   0       0
2010-01-03  2010    1   1   3   1   January Sunday  1   0   0       0
2010-01-04  2010    1   1   4   2   January Monday  1   1   0       0
2010-01-05  2010    1   1   5   3   January Tuesday 1   1   0       0

I plan on creating each monthly calendar using tables.

So the initial part of the code is:

echo '<table>';
$sql="SELECT * FROM sched_calendar_table WHERE y=2013";
$result=mysql_query($sql);
while($row=mysql_query($result)){
 echo '<tr><td>'; // .. unsure how to account for the placeholder days that aren't part of the month being displayed... how to do this?
}
echo '</table>';
KingCrunch
  • 128,817
  • 21
  • 151
  • 173
  • 1
    I answered something similar already > http://stackoverflow.com/questions/12317909/find-weekly-periods-starting-on-a-monday-for-a-month/12324474#12324474 – Glavić Jan 06 '13 at 22:34
  • The `mysql`-extension is outdated, not maintained anymore and will be marked as deprecated with PHP5.5. Use `PDO_MYSQL`, or `MySQLi` instead. See http://php.net/mysql-connect – KingCrunch Jan 06 '13 at 22:36
  • [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – Madara's Ghost Jan 06 '13 at 22:38

1 Answers1

1

Quick function, that returns all dates by weeks, start with monday, end with sunday (even if that dates are from after/before month).

function getWeekDays($month, $year)
{
    $dFrom = new DateTime("$year-$month-01");
    $dTo = clone $dFrom;
    if (($N = $dFrom->format('N')) > 1) {
        $dFrom->modify('-' . ($N - 1) . ' day');
    }
    $dTo->add(new DateInterval('P1M'));
    if (($N = $dTo->format('N')) < 7) {
        $dTo->modify((8 - $N) . ' day');
    }

    $p = new DatePeriod($dFrom, new DateInterval('P1D'), $dTo);

    $datesByWeek = array();
    foreach ($p as $d) {
        $datesByWeek[ $d->format('W') ][] = $d;
    }
    return $datesByWeek;
}

Example:

print_r( getWeekDays(1, 2013) );

Now you have all dates that represent current month, so you can do DB magic...

How to output table from this data, see > Find weekly periods (starting on a Monday) for a month

Community
  • 1
  • 1
Glavić
  • 42,781
  • 13
  • 77
  • 107
  • Thank you for the quick response. I have PHP Version 5.2.6... yet I get a Fatal error: Call to undefined method DateTime::createfromformat() in ... line 36 .. I tried switching it to $dFrom = DateTime::createFromFormat('Y-n-d H', $year."-".$month."-01 00"); but still an error – mandalorianwarrior Jan 06 '13 at 23:43
  • 1
    Yes, code was for >=5.4 version. I updated it, so now it should work for your version. – Glavić Jan 06 '13 at 23:56