簡體   English   中英

如何使用php顯示從當月開始到結束日期的日期?

[英]how to display dates from starting to end date from current month using php?

我可以從mysql中存儲的數據顯示從開始日期到結束日期的日期,但是我想以以下形式顯示從本月1日到本月結束日期的當前月份日期:

1 2 3 4。 31

這可能嗎?

請參閱PHP cal_days_in_month

如這里解釋

此函數將返回指定日歷的一年中的天數。

int cal_days_in_month ( int $calendar , int $month , int $year )

還有一個例子:

$number = cal_days_in_month(CAL_GREGORIAN, 8, 2003); // 31
echo "There were {$number} days in August 2003";

使用循環顯示天數

如果您想要一個月中的所有日子,請嘗試以下循環,其中date(“ t”)為您提供該月的最后一天的數字,我們知道第一天始終為1。

$last = date("t");
for($i=1; $i<= $last; $i++) echo "$i "; 

對於PHP部分,這可能會幫助您:

// Get the current date
$today = getdate();

// Get the number of days in current month
$days_in_month = cal_days_in_month(CAL_GREGORIAN, $today['mon'], $today['year']); 

// Print the dates
for ($i = 1; $i <= $days_in_month; $i++) {
  echo ' ' . $i;
}

樣式和輸出是另一項任務,這只是使您入門。

yes. it is possible.
please, use below php code. it can work for php 4.1 and higher.
<?php
$number = cal_days_in_month(CAL_GREGORIAN, date('m'), date('Y'));
for($i=1;$i<=$number;$i++)
    echo $i.'<br>';
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM