Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lara336223's avatar

date() and foreach loop :~/

Hello. It's a bit not Laravel, but I thought there might be some very good PHP & date() experts here ? Basically, I grab the amount of Days in a Month , $Days. Then put them in a foreach($d =1; 1 < $days+1; $d++) then what I need to do is Grab when $d is Today ! So I can set the class="" in a I have tried all manner of things / searched (google) to No avail. Can anyone Help ? Please ? Thank-You Sid

0 likes
2 replies
LaryAI's avatar

Certainly! You want to loop through all days in a month and highlight the current day (today) by setting a special class in your HTML output.

Here's a clean way to do this in PHP:

<?php
$month = date('m'); // current month
$year = date('Y');  // current year
$days = cal_days_in_month(CAL_GREGORIAN, $month, $year); // total days in month
$today = date('j'); // current day of the month (no leading zeros)

for ($d = 1; $d <= $days; $d++) {
    // Check if this day is today
    $class = ($d == $today) ? 'class="today"' : '';
    echo "<span $class>$d</span> ";
}
?>

Explanation:

  • $days gets the total number of days in the current month.
  • $today gets today's day number.
  • The loop goes from 1 to $days.
  • If $d matches $today, it adds a class="today" to the <span>.
  • Otherwise, it outputs a normal <span>.

You can style the .today class in your CSS as needed.

Let me know if you need this adapted for a specific month/year or in a Blade template!

lara336223's avatar

That nearly worked. In my Calendared script it got May , but also June & July etc. I added this... if($d == $today and $month == date('m',strtotime('m')) and $year == date('Y',strtotime('Y')) ){ but that did Not get Just May :~( Can anyone help Please ? Thank-You Sid

Please or to participate in this conversation.