Avatar of alex4544
alex4544
 asked on

If date is today then else php

Hi,

I am generating a calendar using php and I want to check the date - if the day is today then give the box a red border see the code below where i want my if then statement

if the date is today then give the td a red border if not then don't
if ($time = mktime()) {
        echo '<td class="date" style="border:1px solid red" ' . $colspan . '>' . $new_date_format . '</td>';
		}
		else {
        echo '<td class="date" ' . $colspan . '>' . $new_date_format . '</td>';
		}

Open in new window

PHP

Avatar of undefined
Last Comment
bansidhar

8/22/2022 - Mon
striker46

And the question is...?
CWS (haripriya)

Try this:
if ($time == mktime()) {
        echo '<td class="date" style="border:1px solid red" ' . $colspan . '>' . $new_date_format . '</td>';
		}
		else {
        echo '<td class="date" ' . $colspan . '>' . $new_date_format . '</td>';
		}

Open in new window

bansidhar

it $time is timestamp
if (date('Y-m-d,$time) == date('Y-m-d') ) {
        echo '<td class="date" style="border:1px solid red" ' . $colspan . '>' . $new_date_format . '</td>';
                }
                else {
        echo '<td class="date" ' . $colspan . '>' . $new_date_format . '</td>';
                }

Open in new window

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
bansidhar

missed a quote there
if (date('Y-m-d',$time) == date('Y-m-d') ) {
        echo '<td class="date" style="border:1px solid red" ' . $colspan . '>' . $new_date_format . '</td>';
                }
                else {
        echo '<td class="date" ' . $colspan . '>' . $new_date_format . '</td>';
                }

Open in new window

pkoops

You should realise, that mktime() gives you an long integer (seconds from 00:00 01.01.1970 until now).

so you need to convert both timestamps into day, month and year and compare them.

Take a look on the snippet, there are some samples.
$timestamp = mktime(); // 1235995650
 
$date_today = date('d.m.Y', mktime()); // 02.03.2009
$day = date('d', mktime()); // 02
$month = date('m', mktime()); // 03
$year = date('Y', mktime()); // 2009

Open in new window

alex4544

ASKER
I was only using mktime because I dont know what im doing - non of the suggestions work.

As far as I know I need to check if new_date_format = today? and then display that in red. I have attached the code bfore what i am trying to do

    if ($i <= 6 and $show_weekday[$wday] == "yes") {
        // echo ("<td bgcolor=\"".$datum_color."\" align=\"center\"><span style=\"color:".$datum_font_color."; \">".$this_day3."</span></td>");
        $new_date_format = str_replace("{weekday}", $weekday[$wday], $date_format);
        $new_date_format = str_replace("{day}", $mday, $new_date_format);
        $new_date_format = str_replace("{month}", $month, $new_date_format);
        $new_date_format = str_replace("{year}", $year, $new_date_format);
 
//Make this red for today only
 
        echo '<td class="date" ' . $colspan . '>' . $new_date_format . '</td>';
//End    

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
alex4544

ASKER
Added more code
// Build date row
$new_app_data_merged = $app_data_merged;
for ($i = 0; $i <= 6; $i++) {
    $weekday_timestamp = mktime(0, 0, 0, $selected_month_begin, $selected_day_begin - $weekstart + $hom + $i + $woche, $selected_year_begin);
    list($wday, $mday, $month_1, $month_01, $year) = split("( )", date("w d n m Y", $weekday_timestamp));
    $month_1 -= 1;
 
    if ($year_length == "short") {
        $year = substr ($year, -2);
    }
 
    switch ($month_length) {
        case "short":
            $month = $months_short[$month_1];
            break;
        case "long":
            $month = $months_long[$month_1];
            break;
        case "number":
            $month = $month_01;
    }
 
    switch ($weekday_length) {
        case "short":
            $weekday = $weekday_short;
            break;
        case "long":
            $weekday = $weekday_long;
    }
 
    $this_day3 = $weekday[$wday] . "<br>" . $mday . "." . $month . $year;
 
    if ($i <= 6 and $show_weekday[$wday] == "yes") {
      //   echo ("<td bgcolor=\"".$datum_color."\" align=\"center\"><span style=\"color:".$datum_font_color."; \">".$this_day3."</span></td>");
        $new_date_format = str_replace("{weekday}", $weekday[$wday], $date_format);
        $new_date_format = str_replace("{day}", $mday, $new_date_format);
        $new_date_format = str_replace("{month}", $month, $new_date_format);
        $new_date_format = str_replace("{year}", $year, $new_date_format);
 
//Make this red for today only
 
        echo '<td class="date" ' . $colspan . '>' . $new_date_format . '</td>';
//End    

Open in new window

ASKER CERTIFIED SOLUTION
bansidhar

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
alex4544

ASKER
Thank you Bansidhar that works perfectly
bansidhar

if it works, you can accept and close the question :)
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23