hye..
i have a problem to display only numbers of date according the month automatically. I want to display in the table like this

date 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17............31

Dani AI

Generated

— the goal is to print a single table row labeled "date" followed by columns 1..N where N is the number of days in the selected month (December should show 31). 's answer using PHP date functions is correct; here is a modern, tidy alternative using PHP's DateTime and a simple loop to emit the HTML row so you can plug it into your page and style as needed.

$year  = 2011;
$month = 12; // or set from user input / current month
$dt    = new DateTime("$year-$month-01");
$days  = (int) $dt->format('t');

echo '<table><tr><th scope="col">date</th>';
for ($d = 1; $d <= $days; $d++) {
    echo '<td>' . $d . '</td>';
}
echo '</tr></table>';

Notes and practical tips:

  • DateTime->format('t') handles leap years and returns the correct day count for February. See the PHP DateTime docs for details: PHP DateTime manual.
  • If $month comes from user input validate it (integer 1–12) and cast to int to avoid injection or logic errors.
  • For very wide tables (28–31 columns) wrap the table in a container with overflow-x:auto so it stays usable on small screens. Consider smaller cell padding or a scrollable header if you need the header to remain visible while scrolling.
  • Always ensure the correct timezone (use date_default_timezone_set() if necessary) and escape any dynamic text with htmlspecialchars() when outputting user-supplied values.

Recommended Answers

All 3 Replies

Member Avatar for Member #120589

1. get month and year.
2. get a unix timestamp for the first day of that month using mktime().
3. get the number of days in the month with date('t')
4. Make days into a range with range(1,$no_days)

An all-in-one:

$month = 2; 
$year = 2008;

$days = range(1,date('t',mktime(0,0,0,$month,1,$year)));
print_r($days);

Actually i want to display the dates depends on month.
for example,in this month (december) have 31days so i want to display that days in the html table .i.e

date | 1 | 2 | 3 |.....................|31 |

Member Avatar for Member #120589

It's what I gave you. To display like this:

$month = 2; 
$year = 2008;
 
$days = range(1,date('t',mktime(0,0,0,$month,1,$year)));
echo "|" . implode("|",$days) . "|";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.