我有一年中的完整日期,例如,这里是我将特定日期开始日期,结束日期,日期和工作日作为" 2016-01-01" ," 2016-12-31"," Sunday",0
PHP代码:
$("#flip").click(function() {
$(".flip-target").toggleClass("flip");
});
这里现在我试图通过一周没有,例如我需要星期日的所有日期为一年的第一周然后我需要第一周的日期 或者,如果我需要第1周,第3周,我需要获得1,3周的完整日期,通过上面的代码,我得到了一年中的完整日期,如何获得周基础知识。
示例我试图提供这样的周数:
$start_date = "2016-01-01";
$end_date = "2016-12-31";
$day = "Sunday";
//weekday: Sunday = 0 , Monday = 1 ..only if it is mulitple days
$dateArr = GetSelectdayDatesForCurYear($start_date, $end_date,$day, 0);
function GetSelectdayDatesForCurYear($start_date, $end_date, $selected_day, $weekday)
{
$seltd_weekdays = $selected_day; //even can supply multiple days like "Sunday,Monday,Friday..";
$arr_weekdays = explode(",", $seltd_weekdays);
$weekdaynum = $arr_weekdays[$weekday];
if(!$weekdaynum){ //if Sunday[0],Monday[1] not available
echo "Invalid Weekday!";
}
else
{
$get_start_date = strtotime("+0 day", strtotime($start_date) );
$get_end_date = strtotime($end_date);
$dateArr = array();
$get_date = strtotime($weekdaynum, $get_start_date); //first date for the first month
while($get_date <= $get_end_date) //loop till end date
{
$dateArr[] = date("Y-m-d", $get_date);
$get_date = strtotime("+1 weeks", $get_date);
}
$dateArr[] = date("Y-m-d", $get_date);
return $dateArr;
}
}
如何制作!!!
答案 0 :(得分:1)
您可以使用此方法从第一个月和第二个月获得所有星期日:
// create an array for allowed week as you need 1,2 (use as first,second for )
$allowedWeek = array('first','second');
// this loop will execute two times.
foreach ($allowedWeek as $key => $value)
{
for($i=1;$i<=12;$i++)
{
// start from and end
$start = new DateTime(''.$value.' sunday of 2016-'.$i);
$end = new DateTime('2016-12-31');
$interval = DateInterval::createFromDateString(''.$value.' sunday of next month');
$period = new DatePeriod($start, $interval, $end);
// store date by month wise in an array
$dateArr[$value][] = $period->start->format('Y-m-d');
}
}
echo "<pre>";
print_r($dateArr);
结果是:
Array
(
[first] => Array
(
[0] => 2016-01-03
[1] => 2016-02-07
[2] => 2016-03-06
[3] => 2016-04-03
[4] => 2016-05-01
[5] => 2016-06-05
[6] => 2016-07-03
[7] => 2016-08-07
[8] => 2016-09-04
[9] => 2016-10-02
[10] => 2016-11-06
[11] => 2016-12-04
)
[second] => Array
(
[0] => 2016-01-10
[1] => 2016-02-14
[2] => 2016-03-13
[3] => 2016-04-10
[4] => 2016-05-08
[5] => 2016-06-12
[6] => 2016-07-10
[7] => 2016-08-14
[8] => 2016-09-11
[9] => 2016-10-09
[10] => 2016-11-13
[11] => 2016-12-11
)
)
答案 1 :(得分:0)
完整答案:
以下是我的问题的答案。我通过上面的回答代码得出了答案,感谢'devpro'来帮助我!
$selected_leave_weeks = array(1,2,3,4,5); // array(1,2,3,4) or array(1,5) etc;
//Change the week format to week name for supplying as array to the function.
$week_array = array();
foreach($selected_leave_weeks as $convert_week)
{
switch ($convert_week)
{
case 1:
$convert_week = 'first';
$week_array[] = $convert_week;
break;
case 2:
$convert_week = 'second';
$week_array[] = $convert_week;
break;
case 3:
$convert_week = 'third';
$week_array[] = $convert_week;
break;
case 4:
$convert_week = 'fourth';
$week_array[] = $convert_week;
break;
case 5:
$convert_week = 'last';
$week_array[] = $convert_week;
break;
}
}
//Supply Week names for fetching complete dates you need - supplying weeks as array('first','second','third','fourth','last')
//This gives a complete dates for whole year
function getDateForSpecificWeeks($selected_day, $cur_year, $allowedWeek)
{
foreach ($allowedWeek as $key => $value)
{
for($i=1;$i<=12;$i++)
{
// start from and end
$start = new DateTime(''.$value.' '.$selected_day.' of '.$cur_year.'-'.$i);
$end = new DateTime(''.$cur_year.'-12-31'); // last day is always December 31
$interval = DateInterval::createFromDateString(''.$value.' '.$selected_day.' of next month');
$period = new DatePeriod($start, $interval, $end);
// store date by month wise in an array
$dateArr[] = $period->start->format('Y-m-d');
}
}
return $dateArr;
}
//Supply Parameters for getting dates
$allowed_weeks = $week_array; //From Above code - $week_array contain array('first','second','third','fourth','last')
$selected_day = "Sunday";//Sunday, Monday, ..etc
$cur_year = "2016";//date('Y'); //Takes current Year
//if 4th week is not selected remove dates from 5th Week- "last" mean { because its give last date of the month}
if ((in_array("5", $selected_leave_weeks)) && (!in_array("4", $selected_leave_weeks)))
{
$fourth_week = array('fourth');
$fourth_week_dates = getDateForSpecificWeeks($selected_day, $cur_year, $fourth_week);
$get_config_week_dates = getDateForSpecificWeeks($selected_day, $cur_year, $allowed_weeks);
//array_diff removes 4th week dates contain in 5th
$dates_avail = array_diff($get_config_week_dates, $fourth_week_dates);
//remove a date if dates are twice or more
$datesArr = array_unique($dates_avail);
}
else
{
$get_config_week_dates = getDateForSpecificWeeks($selected_day, $cur_year, $allowed_weeks);
//remove one date if dates are twice or more
$datesArr = array_unique($get_config_week_dates);
}
//you will get your date
echo '<pre>';
print_r($datesArr);
?>
结果:
Array
(
[0] => 2016-01-03
[1] => 2016-02-07
[2] => 2016-03-06
[3] => 2016-04-03
[4] => 2016-05-01
[5] => 2016-06-05
[6] => 2016-07-03
[7] => 2016-08-07
[8] => 2016-09-04
[9] => 2016-10-02
[10] => 2016-11-06
[11] => 2016-12-04
[12] => 2016-01-10
[13] => 2016-02-14
[14] => 2016-03-13
[15] => 2016-04-10
[16] => 2016-05-08
[17] => 2016-06-12
[18] => 2016-07-10
[19] => 2016-08-14
[20] => 2016-09-11
[21] => 2016-10-09
[22] => 2016-11-13
[23] => 2016-12-11
[24] => 2016-01-17
[25] => 2016-02-21
[26] => 2016-03-20
[27] => 2016-04-17
[28] => 2016-05-15
[29] => 2016-06-19
[30] => 2016-07-17
[31] => 2016-08-21
[32] => 2016-09-18
[33] => 2016-10-16
[34] => 2016-11-20
[35] => 2016-12-18
[36] => 2016-01-24
[37] => 2016-02-28
[38] => 2016-03-27
[39] => 2016-04-24
[40] => 2016-05-22
[41] => 2016-06-26
[42] => 2016-07-24
[43] => 2016-08-28
[44] => 2016-09-25
[45] => 2016-10-23
[46] => 2016-11-27
[47] => 2016-12-25
[48] => 2016-01-31
[52] => 2016-05-29
[54] => 2016-07-31
[57] => 2016-10-30
)