www.webdeveloper.com
Results 1 to 14 of 14

Thread: change td cell background based on current date!

  1. #1
    Join Date
    May 2013
    Posts
    7

    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.
    eg
    <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
    Posts
    163
    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");

    if(
    $date == "18 May"){
    echo 
    "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
    Posts
    7

    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
    Posts
    163
    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
    Posts
    7
    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">
    <tr>
    <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>
    </tr>
    <tr>
    <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>
    </tr>
    </table>

  6. #6
    Join Date
    Dec 2011
    Posts
    163
    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
    Posts
    7
    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
    Posts
    163
    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:
    <?php 

    //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

     
    switch($day_of_week){ 

     case 
    "Sun"$blank 0; break; 

     case 
    "Mon"$blank 1; break; 

     case 
    "Tue"$blank 2; break; 

     case 
    "Wed"$blank 3; break; 

     case 
    "Thu"$blank 4; break; 

     case 
    "Fri"$blank 5; break; 

     case 
    "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>";

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

     echo 
    "<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;



     echo 
    "<tr>";

     
    //first we take care of those blank days

     
    while ( $blank 

     { 

     echo 
    "<td></td>"

     
    $blank $blank-1

     
    $day_count++;

     } 

    //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 

     { 

     echo 
    "<td> $day_num </td>"

     
    $day_num++; 

     
    $day_count++;



     
    //Make sure we start a new row every week

     
    if ($day_count 7)

     {

     echo 
    "</tr><tr>";

     
    $day_count 1;

     }

     } 

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

     
    while ( $day_count >&& $day_count <=

     { 

     echo 
    "<td> </td>"

     
    $day_count++; 

     } 

     
     echo 
    "</tr></table>";
    At this section of code, you would change for your background...

    FROM...
    PHP Code:
    while ( $day_num <= $days_in_month 

     { 

     echo 
    "<td> $day_num </td>"

     
    $day_num++; 

     
    $day_count++; 
    TO...
    PHP Code:
    while ( $day_num <= $days_in_month 

     { 

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

     
    $day_num++; 

     
    $day_count++; 
    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
    Posts
    7
    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>";
    }else{
    echo "<td> $day_num </td>";
    }

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

  11. #11
    Join Date
    May 2013
    Posts
    7
    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
    Posts
    163
    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
    Posts
    7
    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.

    Thanks

  14. #14
    Join Date
    Dec 2011
    Posts
    163
    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



Recent Articles