Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8773443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:17:45+00:00 2026-06-13T18:17:45+00:00

This code creates a nice calendar ( original code ), but I’m trying to

  • 0

This code creates a nice calendar (original code), but I’m trying to make some modifications on it.
The first lines are ok, no need to pay attention to that, but here it is:

$calendar = '<table cellpadding="0" cellspacing="0" class="calendar">';
$headings = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$calendar.= '<tr class="calendar-row"><td class="calendar-day-head">'.implode('</td><td class="calendar-day-head">',$headings).'</td></tr>';

$running_day = date('w',mktime(0,0,0,$month,1,$year));
$days_in_month = date('t',mktime(0,0,0,$month,1,$year));
$days_in_this_week = 1;
$day_counter = 0;
$dates_array = array();

$calendar.= '<tr class="calendar-row">';

for($x = 0; $x < $running_day; $x++):
    $calendar.= '<td class="calendar-day-np"> </td>';
    $days_in_this_week++;
endfor;

Here is where I stucked. From now on, the calendar access the database and print my dates based on the events I have recorded. For the dates that the query has found any event, than the code prints a link in it. See the code:

$db = new PDO('mysql:host=localhost;dbname=calendar','root','');
$stmt = $db->prepare('SELECT time, title FROM events');
$stmt->execute();

$rawTimeStamps = $stmt->fetchAll(PDO::FETCH_ASSOC);
$cleanDateArray = array();
foreach ($rawTimeStamps as $t) {
$rawDate = $t['time'];
$rawDate = getdate($rawDate);
$cleanDate = mktime(0,0,0,$rawDate['mon'],$rawDate['mday'],$rawDate['year']);
$cleanDataArray[] = $cleanDate;
}
for($list_day = 1; $list_day <= $days_in_month; $list_day++):

    $calendar.= '<td class="calendar-day">';
        $timestamp = mktime(0,0,0,$month,$list_day,$year);
        if (in_array($timestamp, $cleanDataArray)) {
        $calendar.= '<div class="day-number day-number-event"><a href="#">'.$list_day.'</a></div>';
        } else {
        $calendar.= '<div class="day-number day-number-noevent">'.$list_day.'</div></div><div id="calendar-events"></div>';
        }
    $calendar.= '</td>';
    if($running_day == 6):
        $calendar.= '</tr>';
        if(($day_counter+1) != $days_in_month):
            $calendar.= '<tr class="calendar-row">';
        endif;
        $running_day = -1;
        $days_in_this_week = 0;
    endif;
    $days_in_this_week++; $running_day++; $day_counter++;
endfor;

And than, the code finishes the calendar.

if($days_in_this_week < 8):
    for($x = 1; $x <= (8 - $days_in_this_week); $x++):
        $calendar.= '<td class="calendar-day-np"> </td>';
    endfor;
endif;

$calendar.= '</tr>';

$calendar.= '</table>';

return $calendar;

What I need is to print other infos from the database related to the date that has an event. In other words, all I want is to print the events title (that are also recorded in a column of the events table) right below the event data. Something like:

$calendar.= '<div class="day-number day-number-event"><a id="'.$timestamp.'" href="#">'.$list_day.'</a></div><p>'.$title.'</p>';}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:17:47+00:00Added an answer on June 13, 2026 at 6:17 pm

    After some tests and studies, this is the solution:

    function draw_calendar($month,$year){
    
       $calendar = '<table cellpadding="0" cellspacing="0" class="calendar">';
    
    
       $headings = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
       $calendar.= '<tr class="calendar-row"><td class="calendar-day-head">'.implode('</td><td class="calendar-day-head">',$headings).'</td></tr>';
    
    /* days and weeks vars now ... */
       $running_day = date('w',mktime(0,0,0,$month,1,$year));
       $days_in_month = date('t',mktime(0,0,0,$month,1,$year));
       $days_in_this_week = 1;
       $day_counter = 0;
       $dates_array = array();
    
    /* row for week one */
       $calendar.= '<tr class="calendar-row">';
    
    /* print "blank" days until the first of the current week */
       for($x = 0; $x < $running_day; $x++):
         $calendar.= '<td class="calendar-day-np"> </td>';
         $days_in_this_week++;
       endfor;
    
       $db = new PDO('mysql:host=localhost;dbname=calendar','root','');
    
       $stmt = $db->prepare('SELECT time, title FROM events');
       $stmt->execute();
    
       $rawTimeStamps = $stmt->fetchAll(PDO::FETCH_ASSOC);
       $cleanDateArray = array();
    
       foreach ($rawTimeStamps as $t) {
          $rawDate = $t['time'];
          $rawDate = getdate($rawDate);
          $cleanDate = mktime(0,0,0,$rawDate['mon'],$rawDate['mday'],$rawDate['year']);
          $cleanDataArray[] = $cleanDate;
       }
    
    /* keep going with days.... */
       for($list_day = 1; $list_day <= $days_in_month; $list_day++):
    
        $calendar.= '<td class="calendar-day">';
    
            $timestamp = mktime(0,0,0,$month,$list_day,$year);
    
            if (in_array($timestamp, $cleanDataArray)) {
    
            /* embromation */
            $date = getdate($timestamp);
            $time_start = mktime(0,0,0,$date['mon'],$date['mday'],$date['year']);
            $time_end = mktime(23,59,59,$date['mon'],$date['mday'],$date['year']);
            $stmt = $db->prepare('SELECT title FROM events WHERE time BETWEEN ? AND ?');
            $stmt->bindParam(1,$time_start,PDO::PARAM_INT);
            $stmt->bindParam(2,$time_end,PDO::PARAM_INT);
            $stmt->execute();
            $events = $stmt->fetch(PDO::FETCH_ASSOC);
    
            $calendar.= '<div class="day-number day-number-event"><a href="#">'.$list_day.'</a></div><p>'.$events["title"].'</p>';
            } else {
            $calendar.= '<div class="day-number day-number-noevent">'.$list_day.'</div></div><div id="calendar-events"></div>';
            }
    
        $calendar.= '</td>';
        if($running_day == 6):
            $calendar.= '</tr>';
            if(($day_counter+1) != $days_in_month):
                $calendar.= '<tr class="calendar-row">';
            endif;
            $running_day = -1;
            $days_in_this_week = 0;
        endif;
        $days_in_this_week++; $running_day++; $day_counter++;
    endfor;
    
    /* finish the rest of the days in the week */
    if($days_in_this_week < 8):
        for($x = 1; $x <= (8 - $days_in_this_week); $x++):
            $calendar.= '<td class="calendar-day-np"> </td>';
        endfor;
    endif;
    
    /* final row */
    $calendar.= '</tr>';
    
    /* end the table */
    $calendar.= '</table>';
    
    /* all done, return result */
    return $calendar;
    

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code, which creates an image, and then adds some effects to
I've got this code... seems nice and elegant, but apparently the framework don't like
Well ive got this code that creates a table from JSON data. If a
Hi I have been working on this code that creates a table with radio
I have this JavaScript code that creates a table, and puts in the rows
How can I write this code more cleanly/concisely? /// <summary> /// Creates a set
I have this function in a Code Igniter model that creates a new video.
I am creating a custom code generator using c#... this generator now only creates
I'm trying to decide if this is not very nice, or not. Basically, I
This is my first post, so please be nice :) I have a question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.