how to get both days and dates for selected month of selected year and show in tables. e-g: i have tried so far.
如何获取所选年份的选定月份的日期和日期并显示在表格中。 e-g:到目前为止我已经尝试过了。
<?php
$num_of_days = cal_days_in_month(CAL_GREGORIAN, 9, 2003);
for( $i=1; $i<= $num_of_days; $i++)
$dates[]= str_pad($i,2,'0', STR_PAD_LEFT);
/*echo "<pre>";
print_r($dates);
echo "</pre>";*/
?>
<table>
<tr>
<?php
foreach($dates as $date){
echo"<td>".$date."</td>";
}
?>
</tr>
</table>
this executes for me this code.
这为我执行了这段代码。
<table>
<tbody><tr>
<td>01</td><td>02</td><td>03</td><td>04</td><td>05</td><td>06</td><td>07</td><td>08</td><td>09</td><td>10</td><td>11</td><td>12</td><td>13</td><td>14</td><td>15</td><td>16</td><td>17</td><td>18</td><td>19</td><td>20</td><td>21</td><td>22</td><td>23</td><td>24</td><td>25</td><td>26</td><td>27</td><td>28</td><td>29</td><td>30</td> </tr>
</tbody></table>
but i want another row below the dates row. which should show day related to that date??
但我想要在日期行下面另一行。哪个应该显示与该日期相关的日子?
by day i mean :monday, tues, wed etc. by date i mean : 1, 2, 3, 4 etc
白天我的意思是:星期一,星期二,星期三,按日期我的意思是:1,2,3,4等
so it would be like
所以它会是这样的
<tr><td>1</td><td>2</td><td>3</td><td>4</td>
<tr><td>Mon</td><td>Tues</td><td>Wed</td><td>Thursday</td>
I hope i could explain my self..
我希望我可以解释我的自我..
8 个解决方案
#1
10
You can use date('l')
to get the corresponding day name:
您可以使用日期('l')来获取相应的日期名称:
<?php
$date = '2003-09-01';
$end = '2003-09-' . date('t', strtotime($date)); //get end date of month
?>
<table>
<tr>
<?php while(strtotime($date) <= strtotime($end)) {
$day_num = date('d', strtotime($date));
$day_name = date('l', strtotime($date));
$date = date("Y-m-d", strtotime("+1 day", strtotime($date)));
echo "<td>$day_num <br/> $day_name</td>";
}
?>
</tr>
</table>
#2
8
A different approach could be to use the DateTime object:
一种不同的方法可能是使用DateTime对象:
$aDates = array();
$oStart = new DateTime('2014-12-01');
$oEnd = clone $oStart;
$oEnd->add(new DateInterval("P1M"));
while ($oStart->getTimestamp() < $oEnd->getTimestamp()) {
$aDates[] = $oStart->format('D d');
$oStart->add(new DateInterval("P1D"));
}
Then to print:
然后打印:
foreach ($aDates as $day) {
echo $day;
}
For more information about the format parameters, you can refer to: http://php.net/manual/en/function.date.php
有关格式参数的更多信息,请参阅:http://php.net/manual/en/function.date.php
#3
4
You can try something like this
你可以尝试这样的事情
$myYearMonth = '2003-09';
$start = new DateTime(date('Y-m-01', strtotime($myYearMonth)));
$end = new DateTime(date('Y-m-t', strtotime($myYearMonth)));
$diff = DateInterval::createFromDateString('1 day');
$periodStart = new DatePeriod($start, $diff, $end);
foreach ( $periodStart as $dayDate ){
echo '<td>'.$dayDate->format( "d\n" ).'</td><td>'.$dayDate->format( "l\n" ).'</td>';
}
#4
3
You shouldn't use the date()
function as the date range is limited to 1970-2038 according to the PHP documentation :
您不应该使用date()函数,因为根据PHP文档,日期范围限制为1970-2038:
The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer). However, before PHP 5.1.0 this range was limited from 01-01-1970 to 19-01-2038 on some systems (e.g. Windows).
时间戳的有效范围通常是格林威治标准时间1901年12月13日20:45:54格林尼治标准时间周二,2038年1月19日格林威治标准时间03:14:07。 (这些是对应于32位有符号整数的最小值和最大值的日期)。但是,在PHP 5.1.0之前,在某些系统(例如Windows)上,此范围限制在01-01-1970至19-01-2038之间。
Using the DateTime()
class you can retrieve the information you need for a given month using the following function. This uses DateTime::format()
to get the day name in English (no localization).
使用DateTime()类,您可以使用以下函数检索给定月份所需的信息。这使用DateTime :: format()来获取英文的日期名称(无本地化)。
function getMonth($year, $month) {
// this calculates the last day of the given month
$last=cal_days_in_month(CAL_GREGORIAN, $month, $year);
$date=new DateTime();
$res=Array();
// iterates through days
for ($day=1;$day<=$last;$day++) {
$date->setDate($year, $month, $day);
$res[$day]=$date->format("l");
}
return $res;
}
This will return an associated array like the following:
这将返回一个关联的数组,如下所示:
$res=getMonth(2015, 2);
print_r($res);
Array
(
[1] => Sunday
[2] => Monday
[3] => Tuesday
[4] => Wednesday
[5] => Thursday
[...]
)
To output the data in a two-row table, you can use the following code:
要在两行表中输出数据,可以使用以下代码:
<?php
echo '<table><tr><td>'.implode('</td><td>', array_keys($res)).'</td></tr>';
echo '<tr><td>'.implode('</td><td>', $res).'</td></tr></table>';
As the Datetime::format()
function does not support locales for translation, you can use an associative array to get the translation in another language.
由于Datetime :: format()函数不支持翻译的语言环境,因此您可以使用关联数组来获取另一种语言的翻译。
#5
3
Full solution below.
完整解决方案如下
I determine the number of days in the given month/year combination. Then I loop over the days and create the two required rows at the same time.
我确定给定月/年组合中的天数。然后我循环过去几天同时创建两个必需的行。
When done, the two rows are wrapped in a table and returned to the caller.
完成后,将两行包装在表中并返回给调用者。
<?php
echo buildDate(12, 2017);
function buildDate($month, $year)
{
// start with empty results
$resultDate = "";
$resultDays = "";
// determine the number of days in the month
$daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year);
for ($i = 1; $i <= $daysInMonth; $i++)
{
// create a cell for the day and for the date
$resultDate .= "<td>".sprintf('%02d', $i)."</td>";
$resultDays .= "<td>".date("l", mktime(0, 0, 0, $month, $i, $year))."</td>";
}
// return the result wrapped in a table
return "<table>".PHP_EOL.
"<tr>".$resultDate."</tr>".PHP_EOL.
"<tr>".$resultDays."</tr>".PHP_EOL.
"</table>";
}
?>
phpfiddle link: http://phpfiddle.org/main/code/ffjm-hqsu
phpfiddle链接:http://phpfiddle.org/main/code/ffjm-hqsu
#6
2
Try this script:
试试这个脚本:
// Day of month, e.g. 2014-12-14 if you need the table for december 2014
$date = time();
// Array containing the dates and weekdays
$days = array();
// loop to populate the array
for(
$day = strtotime('midnight', strtotime(date('1 F Y', $date))); /* first day of month */
$day < strtotime(date('1 F Y', strtotime('next month', $date))); /* first day of next month */
$day = strtotime('next day', $day)
){
// insert current day into the array
$days[date('d', $day)] = date('l', $day);
}
// print the row containing all day numbers
echo '<tr><td>'.implode('</td><td>', array_keys($days)).'</td></tr>';
// print the row containing all weekday names
echo '<tr><td>'.implode('</td><td>', $days).'</td></tr>';
#7
1
In your case date('D', strtotime($date))
should work, but you need the date to be in format yyyy-mm-dd
在您的情况下日期('D',strtotime($ date))应该有效,但您需要格式为yyyy-mm-dd的日期
I have done some tests so result:
我做了一些测试,结果如下:
for( $i=1; $i<= $num_of_days; $i++){
$dates[]= str_pad($i,2,'0', STR_PAD_LEFT);
$d = "2003-09-".$i;
$days[] = date('D', strtotime($d));
}
Added another tr
for days:
添加另一个tr天:
<tr>
<?php
foreach($days as $day){
echo"<td>".$day."</td>";
}
?>
</tr>
#8
1
A DateTime method that builds an assoc array of date -> day.
一个DateTime方法,它构建一个date - > day的assoc数组。
The results for December 2015 look like this:
2015年12月的结果如下:
array(31) {
[1] = string(7) "Tuesday"
[2] = string(9) "Wednesday"
[3] = string(8) "Thursday"
[4] = string(6) "Friday"
[5] = string(8) "Saturday"
[6] = string(6) "Sunday"
[7] = string(6) "Monday"
[8] = string(7) "Tuesday"
[9] = string(9) "Wednesday"
[10] = string(8) "Thursday"
[11] = string(6) "Friday"
[12] = string(8) "Saturday"
[13] = string(6) "Sunday"
[14] = string(6) "Monday"
[15] = string(7) "Tuesday"
[16] = string(9) "Wednesday"
[17] = string(8) "Thursday"
[18] = string(6) "Friday"
[19] = string(8) "Saturday"
[20] = string(6) "Sunday"
[21] = string(6) "Monday"
[22] = string(7) "Tuesday"
[23] = string(9) "Wednesday"
[24] = string(8) "Thursday"
[25] = string(6) "Friday"
[26] = string(8) "Saturday"
[27] = string(6) "Sunday"
[28] = string(6) "Monday"
[29] = string(7) "Tuesday"
[30] = string(9) "Wednesday"
[31] = string(8) "Thursday"
}
The full code to get your desired table:
获取所需表格的完整代码:
<?php
// Get an array of days
$arrayDays = getDays(12, 2015);
// Function to get an array of days
function getDays($month, $year){
// Start of Month
$start = new DateTime("{$year}-{$month}-01");
$month = $start->format('F');
// Prepare results array
$results = array();
// While same month
while($start->format('F') == $month){
// Add to array
$day = $start->format('l');
$date = $start->format('j');
$results[$date] = $day;
// Next Day
$start->add(new DateInterval("P1D"));
}
// Return results
return $results;
}
?>
<!-- Output the Table -->
<table>
<tr>
<?php foreach (array_keys($arrayDays) as $someDate): ?>
<td><?= $someDate; ?></td>
<?php endforeach; ?>
</tr>
<tr>
<?php foreach (array_values($arrayDays) as $someDate): ?>
<td><?= $someDate; ?></td>
<?php endforeach; ?>
</tr>
</table>
#1
10
You can use date('l')
to get the corresponding day name:
您可以使用日期('l')来获取相应的日期名称:
<?php
$date = '2003-09-01';
$end = '2003-09-' . date('t', strtotime($date)); //get end date of month
?>
<table>
<tr>
<?php while(strtotime($date) <= strtotime($end)) {
$day_num = date('d', strtotime($date));
$day_name = date('l', strtotime($date));
$date = date("Y-m-d", strtotime("+1 day", strtotime($date)));
echo "<td>$day_num <br/> $day_name</td>";
}
?>
</tr>
</table>
#2
8
A different approach could be to use the DateTime object:
一种不同的方法可能是使用DateTime对象:
$aDates = array();
$oStart = new DateTime('2014-12-01');
$oEnd = clone $oStart;
$oEnd->add(new DateInterval("P1M"));
while ($oStart->getTimestamp() < $oEnd->getTimestamp()) {
$aDates[] = $oStart->format('D d');
$oStart->add(new DateInterval("P1D"));
}
Then to print:
然后打印:
foreach ($aDates as $day) {
echo $day;
}
For more information about the format parameters, you can refer to: http://php.net/manual/en/function.date.php
有关格式参数的更多信息,请参阅:http://php.net/manual/en/function.date.php
#3
4
You can try something like this
你可以尝试这样的事情
$myYearMonth = '2003-09';
$start = new DateTime(date('Y-m-01', strtotime($myYearMonth)));
$end = new DateTime(date('Y-m-t', strtotime($myYearMonth)));
$diff = DateInterval::createFromDateString('1 day');
$periodStart = new DatePeriod($start, $diff, $end);
foreach ( $periodStart as $dayDate ){
echo '<td>'.$dayDate->format( "d\n" ).'</td><td>'.$dayDate->format( "l\n" ).'</td>';
}
#4
3
You shouldn't use the date()
function as the date range is limited to 1970-2038 according to the PHP documentation :
您不应该使用date()函数,因为根据PHP文档,日期范围限制为1970-2038:
The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer). However, before PHP 5.1.0 this range was limited from 01-01-1970 to 19-01-2038 on some systems (e.g. Windows).
时间戳的有效范围通常是格林威治标准时间1901年12月13日20:45:54格林尼治标准时间周二,2038年1月19日格林威治标准时间03:14:07。 (这些是对应于32位有符号整数的最小值和最大值的日期)。但是,在PHP 5.1.0之前,在某些系统(例如Windows)上,此范围限制在01-01-1970至19-01-2038之间。
Using the DateTime()
class you can retrieve the information you need for a given month using the following function. This uses DateTime::format()
to get the day name in English (no localization).
使用DateTime()类,您可以使用以下函数检索给定月份所需的信息。这使用DateTime :: format()来获取英文的日期名称(无本地化)。
function getMonth($year, $month) {
// this calculates the last day of the given month
$last=cal_days_in_month(CAL_GREGORIAN, $month, $year);
$date=new DateTime();
$res=Array();
// iterates through days
for ($day=1;$day<=$last;$day++) {
$date->setDate($year, $month, $day);
$res[$day]=$date->format("l");
}
return $res;
}
This will return an associated array like the following:
这将返回一个关联的数组,如下所示:
$res=getMonth(2015, 2);
print_r($res);
Array
(
[1] => Sunday
[2] => Monday
[3] => Tuesday
[4] => Wednesday
[5] => Thursday
[...]
)
To output the data in a two-row table, you can use the following code:
要在两行表中输出数据,可以使用以下代码:
<?php
echo '<table><tr><td>'.implode('</td><td>', array_keys($res)).'</td></tr>';
echo '<tr><td>'.implode('</td><td>', $res).'</td></tr></table>';
As the Datetime::format()
function does not support locales for translation, you can use an associative array to get the translation in another language.
由于Datetime :: format()函数不支持翻译的语言环境,因此您可以使用关联数组来获取另一种语言的翻译。
#5
3
Full solution below.
完整解决方案如下
I determine the number of days in the given month/year combination. Then I loop over the days and create the two required rows at the same time.
我确定给定月/年组合中的天数。然后我循环过去几天同时创建两个必需的行。
When done, the two rows are wrapped in a table and returned to the caller.
完成后,将两行包装在表中并返回给调用者。
<?php
echo buildDate(12, 2017);
function buildDate($month, $year)
{
// start with empty results
$resultDate = "";
$resultDays = "";
// determine the number of days in the month
$daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year);
for ($i = 1; $i <= $daysInMonth; $i++)
{
// create a cell for the day and for the date
$resultDate .= "<td>".sprintf('%02d', $i)."</td>";
$resultDays .= "<td>".date("l", mktime(0, 0, 0, $month, $i, $year))."</td>";
}
// return the result wrapped in a table
return "<table>".PHP_EOL.
"<tr>".$resultDate."</tr>".PHP_EOL.
"<tr>".$resultDays."</tr>".PHP_EOL.
"</table>";
}
?>
phpfiddle link: http://phpfiddle.org/main/code/ffjm-hqsu
phpfiddle链接:http://phpfiddle.org/main/code/ffjm-hqsu
#6
2
Try this script:
试试这个脚本:
// Day of month, e.g. 2014-12-14 if you need the table for december 2014
$date = time();
// Array containing the dates and weekdays
$days = array();
// loop to populate the array
for(
$day = strtotime('midnight', strtotime(date('1 F Y', $date))); /* first day of month */
$day < strtotime(date('1 F Y', strtotime('next month', $date))); /* first day of next month */
$day = strtotime('next day', $day)
){
// insert current day into the array
$days[date('d', $day)] = date('l', $day);
}
// print the row containing all day numbers
echo '<tr><td>'.implode('</td><td>', array_keys($days)).'</td></tr>';
// print the row containing all weekday names
echo '<tr><td>'.implode('</td><td>', $days).'</td></tr>';
#7
1
In your case date('D', strtotime($date))
should work, but you need the date to be in format yyyy-mm-dd
在您的情况下日期('D',strtotime($ date))应该有效,但您需要格式为yyyy-mm-dd的日期
I have done some tests so result:
我做了一些测试,结果如下:
for( $i=1; $i<= $num_of_days; $i++){
$dates[]= str_pad($i,2,'0', STR_PAD_LEFT);
$d = "2003-09-".$i;
$days[] = date('D', strtotime($d));
}
Added another tr
for days:
添加另一个tr天:
<tr>
<?php
foreach($days as $day){
echo"<td>".$day."</td>";
}
?>
</tr>
#8
1
A DateTime method that builds an assoc array of date -> day.
一个DateTime方法,它构建一个date - > day的assoc数组。
The results for December 2015 look like this:
2015年12月的结果如下:
array(31) {
[1] = string(7) "Tuesday"
[2] = string(9) "Wednesday"
[3] = string(8) "Thursday"
[4] = string(6) "Friday"
[5] = string(8) "Saturday"
[6] = string(6) "Sunday"
[7] = string(6) "Monday"
[8] = string(7) "Tuesday"
[9] = string(9) "Wednesday"
[10] = string(8) "Thursday"
[11] = string(6) "Friday"
[12] = string(8) "Saturday"
[13] = string(6) "Sunday"
[14] = string(6) "Monday"
[15] = string(7) "Tuesday"
[16] = string(9) "Wednesday"
[17] = string(8) "Thursday"
[18] = string(6) "Friday"
[19] = string(8) "Saturday"
[20] = string(6) "Sunday"
[21] = string(6) "Monday"
[22] = string(7) "Tuesday"
[23] = string(9) "Wednesday"
[24] = string(8) "Thursday"
[25] = string(6) "Friday"
[26] = string(8) "Saturday"
[27] = string(6) "Sunday"
[28] = string(6) "Monday"
[29] = string(7) "Tuesday"
[30] = string(9) "Wednesday"
[31] = string(8) "Thursday"
}
The full code to get your desired table:
获取所需表格的完整代码:
<?php
// Get an array of days
$arrayDays = getDays(12, 2015);
// Function to get an array of days
function getDays($month, $year){
// Start of Month
$start = new DateTime("{$year}-{$month}-01");
$month = $start->format('F');
// Prepare results array
$results = array();
// While same month
while($start->format('F') == $month){
// Add to array
$day = $start->format('l');
$date = $start->format('j');
$results[$date] = $day;
// Next Day
$start->add(new DateInterval("P1D"));
}
// Return results
return $results;
}
?>
<!-- Output the Table -->
<table>
<tr>
<?php foreach (array_keys($arrayDays) as $someDate): ?>
<td><?= $someDate; ?></td>
<?php endforeach; ?>
</tr>
<tr>
<?php foreach (array_values($arrayDays) as $someDate): ?>
<td><?= $someDate; ?></td>
<?php endforeach; ?>
</tr>
</table>