PHP:关于此代码的帮助,用于获取一个月中一周中的所有日期
我的代码需要获得一个月的第一天和最后一天的周数,我的代码正在工作,但在某些月份,最后一周的第一天和最后一天将成为下个月的第一天和当前月份的最后一天的组合。一周的开始是星期天 输入是Y-m-d格式的任何日期,例如2014-01-05,这一天并不重要,因为它将转换为该月的第一天 这是月份为1月份时出现问题的输出: 这是月份为2月份时的输出,这是正确的: 我正在使用的函数:PHP:关于此代码的帮助,用于获取一个月中一周中的所有日期,php,date,Php,Date,我的代码需要获得一个月的第一天和最后一天的周数,我的代码正在工作,但在某些月份,最后一周的第一天和最后一天将成为下个月的第一天和当前月份的最后一天的组合。一周的开始是星期天 输入是Y-m-d格式的任何日期,例如2014-01-05,这一天并不重要,因为它将转换为该月的第一天 这是月份为1月份时出现问题的输出: 这是月份为2月份时的输出,这是正确的: 我正在使用的函数: function getWeeks($from) { $array = array(); $f
function getWeeks($from) {
$array = array();
$from = date("Y-m-d", strtotime($from));
$start_date = date('Y-m-01', strtotime($from));
$end_date = date('Y-m-t', strtotime($from));
$end_date1 = date('Y-m-d', strtotime($end_date." + 6 days"));
$week_array = array();
for($date = $start_date; $date < $end_date1; $date = date('Y-m-d', strtotime($date. ' + 7 days')))
{
$getarray = getWeekDates($date, $start_date, $end_date);
$week_array[] = $getarray;
}
return $week_array;
}
function getWeekDates($date, $start_date, $end_date)
{
$week = date('W', strtotime($date));
$year = date('Y', strtotime($date));
$from = date("Y-m-d", strtotime("{$year}-W{$week}+3"));
if($from < $start_date) $from = $start_date;
$to = date("Y-m-d", strtotime("{$year}-W{$week}-6"));
if($to > $end_date) $to = $end_date;
$obj = new stdClass();
$obj->from = $from;
$obj->to = $to;
return $obj;
}
我查看了,但它无助于回答我的问题,因为它使用了不同的实现。我想使用我自己的实现。我已经粗略地编写了一个脚本,这可能会有所帮助,您需要像以前一样将它放入一个类中,或者它可能会帮助您发现代码中的问题。不管怎样,我都希望能有所帮助 PS我讨厌和约会
$dateToday = date('Y-m');
$monthToday = date('m');
$yearToday = date('y');
$totalDaysInMonth = cal_days_in_month(CAL_GREGORIAN, $monthToday, $yearToday);
$dateOfFirstSunday = date('l Y-m-d', strtotime($dateToday . " first Sunday"));
$dayOfFirstSunday = date('j', strtotime($dateToday . " first Sunday"));
for($n = 1; $n < $dayOfFirstSunday; $n++){
echo date('l Y-m-d', strtotime($dateToday . '-' . $n)) . "<br>";
}
echo $dateOfFirstSunday . "<br>";
for($n = $dayOfFirstSunday + 7; $n <= $totalDaysInMonth; $n +=7){
$lastN = $n;
echo date('l Y-m-d', strtotime($yearToday . '-' . $monthToday . '-' . $n)) . "<br>";
}
for($n = $lastN+1; $n <= $totalDaysInMonth; $n++){
echo date('l Y-m-d', strtotime($dateToday . '-' . $n)) . "<br>";
}
在getWeeks函数中:
for($date = $start_date; $date < $end_date1; $date = date('Y-m-d', strtotime($date. ' + 7 days')))
{
$getarray = getWeekDates($date, $start_date, $end_date);
if($getarray) $week_array[] = $getarray; // change it to this
}
$from = date("Y-m-d", strtotime("{$year}-W{$week}+3"));
if($from > $end-date) return null; // Add this
if($from < $start_date) $from = $start_date;
$to = date("Y-m-d", strtotime("{$year}-W{$week}-6"));
在getWeekDates函数中:
for($date = $start_date; $date < $end_date1; $date = date('Y-m-d', strtotime($date. ' + 7 days')))
{
$getarray = getWeekDates($date, $start_date, $end_date);
if($getarray) $week_array[] = $getarray; // change it to this
}
$from = date("Y-m-d", strtotime("{$year}-W{$week}+3"));
if($from > $end-date) return null; // Add this
if($from < $start_date) $from = $start_date;
$to = date("Y-m-d", strtotime("{$year}-W{$week}-6"));