Results 1 to 14 of 14

Thread: change td cell background based on current date!

  1. #1
    Join Date
    May 2013

    change td cell background based on current date!

    This simple one has eluded me for a while.
    I have a basic table with the date for one week in each row.
    <td>14 May<br>Tues</td>
    <td>15 May<br>Wed</td>
    <td>16 May<br>Thurs</td>
    <td>17 May<br>Fri</td>
    <td>18 May<br>Sat</td>
    <td>19 May<br>Sun</td>
    <td>20 May<br>Mon</td>
    <td>21 May<br>Tues</td>
    <td>22 May<br>Wed</td>

    I want todays date to highlight with a different color background to the rest.
    Page is built in php.

  2. #2
    Join Date
    Dec 2011
    I've done something like this before, except with months, but it's the same concept...

    Use the date() function of php.

    PHP Code:
    date("d"// will return a 2-digit 01-31 date
    date("F"// will return the full name of the month like January, with first letter uppercase.
    date("m"// will return the short version of the month name such as Jan, Feb, Mar, Apr, etc. 
    date() uses case-sensitive letters, F is different than f.

    By combining two of these (d and F), you can get today's date...
    PHP Code:
    date("d F"// returns 18 May (according to date of this post) 
    You can use this date() function to make several different date formats, such as May 18, 2013 would be date("F d, Y")

    Now you can use this to compare against your calendar...

    PHP Code:
    $date date("d F");

    $date == "18 May"){
    "style=\"background:rgba(0,0,0,1);\"" // don't forget to escape quotes and such

    is that enough info?

  3. #3
    Join Date
    May 2013

    Trying but....

    Many thanks for your reply Dragonfire2008

    I hate being the idiot in the forum, but my knowledge of PHP is so small, I tried just entering the code into my PHP page to see how it worked before looking at adapting it for the table, but couldn't get it to display anything at all.
    Perhaps just a little more info pse Dragonfire2008?

  4. #4
    Join Date
    Dec 2011
    Post your entire table code and I will see what I can do...

    EDIT: Check out this link and try to understand it first, the code I posted above would go somewhere in there.

    I use this code to generate a calendar, then while it's generating it, it looks in my database each time for an event associated with that date, and highlights the background if there is one and so forth.

    Instead of using "stye", you could also use a class as well...
    Last edited by Dragonfire2008; 05-23-2013 at 08:22 AM.

  5. #5
    Join Date
    May 2013
    Thanks Dragonfire2008 I started working through the link you provided but the final link displaying the total code didn't work. Looked like it had been hacked.

    My weekly table as a php include, it goes from the Tuesday of each week to the Wednesday of the following week. So just one week is to be displayed. (9 days)
    Currently I have the date and day in a different TD but they can easily go into the same one with a <br> between.

    <table width="100%" border="0" class="sessiontimedates">
    <td bgcolor="#FFA275" scope="row">Tues</td>
    <td bgcolor="#FFA275">Wed</td>
    <td bgcolor="#999999" scope="row">Thur</td>
    <td bgcolor="#999999">Fri</td>
    <td bgcolor="#999999">Sat</td>
    <td bgcolor="#999999">Sun</td>
    <td bgcolor="#999999">Mon</td>
    <td bgcolor="#999999">Tues</td>
    <td bgcolor="#999999">Wed</td>
    <td bgcolor="#FFA275" scope="row">21 May</td>
    <td bgcolor="#FFA275">22 May</td>
    <td bgcolor="#999999" scope="row">23 May</td>
    <td bgcolor="#999999">24 May</td>
    <td bgcolor="#999999">25 May</td>
    <td bgcolor="#999999">26 May</td>
    <td bgcolor="#999999">27 May</td>
    <td bgcolor="#999999">28 May</td>
    <td bgcolor="#999999">29 May</td>

  6. #6
    Join Date
    Dec 2011
    So do you mean the calendar would go from the current day as the first day to 9 days later, like friday to next saturday?

  7. #7
    Join Date
    May 2013
    No the days of the week remain static, never change. Only the dates are changed by me once each week on a Monday night. So the highlighted background would move from one day to the next. This table is a movie guide and I want customers eyes drawn to the current date of the week. There are on average a dozen of these tables repeated down the page within this include. Hope this helps give you a better picture of what Im trying to do...

  8. #8
    Join Date
    Dec 2011
    when you say that you change the dates once each week, you mean update the info for the next week/month, right?

    Here is the full amount of code from that PHP Calendar i linked you to...

    PHP Code:

    //This gets today's date
    $date =time () ;

    //This puts the day, month, and year in seperate variables
    $day date('d'$date) ;
    $month date('m'$date) ;
    $year date('Y'$date) ;

    //Here we generate the first day of the month
    $first_day mktime(0,0,0,$month1$year) ;

    //This gets us the month name
    $title date('F'$first_day) ; 

    //Here we find out what day of the week the first day of the month falls on 
    $day_of_week date('D'$first_day) ; 

    //Once we know what day of the week it falls on, we know how many blank days occure before it. If the first day of the week is a Sunday then it would be zero


    "Sun"$blank 0; break; 

    "Mon"$blank 1; break; 

    "Tue"$blank 2; break; 

    "Wed"$blank 3; break; 

    "Thu"$blank 4; break; 

    "Fri"$blank 5; break; 

    "Sat"$blank 6; break; 


    //We then determine how many days are in the current month

    $days_in_month cal_days_in_month(0$month$year) ; 

    //Here we start building the table heads 

    echo "<table border=1 width=294>";

    "<tr><th colspan=7> $title $year </th></tr>";

    "<tr><td width=42>S</td><td width=42>M</td><td 
    width=42>T</td><td width=42>W</td><td width=42>T</td><td 
    width=42>F</td><td width=42>S</td></tr>"

    //This counts the days in the week, up to 7

    $day_count 1;


    //first we take care of those blank days

    while ( $blank 



    $blank $blank-1



    //sets the first day of the month to 1 

    $day_num 1;

    //count up the days, untill we've done all of them in the month

    while ( $day_num <= $days_in_month 


    "<td> $day_num </td>"



    //Make sure we start a new row every week

    if ($day_count 7)



    $day_count 1;



    //Finaly we finish out the table with some blank details if needed

    while ( $day_count >&& $day_count <=


    "<td> </td>"



    At this section of code, you would change for your background...

    PHP Code:
    while ( $day_num <= $days_in_month 


    "<td> $day_num </td>"


    PHP Code:
    while ( $day_num <= $days_in_month 


    $day_num == $date("j")){
    "<td class=\"highlight\"> $day_num </td>";
    "<td> $day_num </td>"


    Notice that I used J instead of d in the date(). d returns date with leading zeros (01, 04, 07), so I used j instead, no leading zeros (1, 3, 7). You might also want to use j at the top where the variables are set, too.

    Need anymore help? I sure you know what to do with the "highlight" class...

  9. #9
    Join Date
    May 2013
    Neato, Im making some progress. Yes its developing into what Im after. I do get an error

    Fatal error: Function name must be a string in url etc

    Its relates to this sequence (top line)

    if($day_num == $date("j")){
    echo "<td class=\"highlight\"> $day_num </td>";
    echo "<td> $day_num </td>";

  10. #10
    Join Date
    Dec 2011
    Oops, I meant date not $date

  11. #11
    Join Date
    May 2013
    Thanks Draginfire2008, that's works a treat. It displays a full month and yes the highlight works great too.
    Is it difficult to make it display only one row of 9 TDs ? Tuesday to the following weeks Wednesday ? As per the table above.

  12. #12
    Join Date
    Dec 2011
    I'm sure you could figure it out by yourself, the author of that code has comments at almost every line, if not, you can always go back to the page that was linked earlier.

    There is plenty of info here for you to make it, and I'm sure you are capable of doing it.

    At least try and lemme know where you are having problems, you won't excel at code unless you try to understand it

  13. #13
    Join Date
    May 2013
    Thank you for your help Dragonfire2008 I know you are right.

    Ill give it a good go and maybe get back to you in a week or two if Im stuck.


  14. #14
    Join Date
    Dec 2011
    Sounds like a plan...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.12758 seconds
  • Memory Usage 3,029KB
  • Queries Executed 15 (?)
More Information
Template Usage (33):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (6)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (14)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (14)postbit
  • (14)postbit_onlinestatus
  • (14)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (73):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates