Common date Operations
- /**
- * Get the start date of all weeks of the year
- * @param $year format ' YYYY '
- * Returns the two-dimensional array subscript key1 value corresponds to the actual year of the week of the year Key2 value corresponds to the number of weeks of the year in which the day is located [the week ordinal of the year]
- * Return False Date format error
- */
- function Get_all_weeks ($year) {
- $week _arr = Array ();
- $year = Intval (Trim ($year));
- $preg = "/^\d{4,}$/";
- if (!preg_match ($preg, $year)) {
- return false;
- }
- $begin _day = $year. '-01-01 ';
- $end _day = $year. '-12-31 ';
- First Monday of the year
- if (Date (' W ', Strtotime ($begin _day))!=1) {
- $begin _day = Date (' y-m-d ', Strtotime ("Next Monday", Strtotime ($begin _day)));
- }
- Number of weeks in first Monday
- $begin _week_num = intval (Date (' W ', Strtotime ($begin _day));
- One last Sunday of the year
- if (Date (' W ', Strtotime ($end _day))!=0) {
- $end _day = Date (' y-m-d ', Strtotime ("Last Sunday", Strtotime ($end _day)));
- There is a cross-year week, a cross-year week on Monday
- $end _day_next = Date (' y-m-d ', Strtotime ($end _day) +24*60*60);
- Year of year and number of weeks in which the week is spanned
- $stride _year = date (' O ', Strtotime ($end _day_next));
- $stride _weeknum = intval (Date (' W ', Strtotime ($end _day_next));
- }
- Number of weeks last Sunday
- $end _week_num = intval (Date (' W ', Strtotime ($end _day));
- The first Monday of the year is the first week or the second week of the year.
- if ($begin _week_num!=1) {
- $i = 2;
- }else{
- $i = 1;
- }
- $j = 0;
- for ($i; $i <= $end _week_num; $i + +) {
- $start _date = Date ("y-m-d", Strtotime ("$begin _day $j Week"));
- $end _day = Date ("y-m-d", Strtotime ($start _date. ' +6 Day ');
- $week _arr[$year [$i] = Array (
- $start _date,
- $end _day
- );
- $j + +;
- }
- if ($end _day_next) {
- $week _arr[$stride _year][$stride _weeknum] = Array (
- $end _day_next,
- Date ("Y-m-d", Strtotime ($end _day_next. ' +6 Day '))
- );
- }
- return $week _arr;
- }
- Demo_ Call
- /*
- $year = ' 2013 ';
- if (Get_all_weeks ($year)) {
- Var_dump (Get_all_weeks ($year));
- }else{
- Echo ' date format error ';
- }
- */
- /**
- * Gets the start date of the week in which the day is located
- * Dependent function Get_all_weeks
- * @param $day format: ' Yyyy-mm-dd '
- * Return False Date format error
- * Correct, return JSON "{" Begin_day ":" Yyyy-mm-dd "," End_day ":" Yyyy-mm-dd "}"
- */
- function Get_day_week ($day) {
- $date _arr = Explode ('-', trim ($day));
- if (!checkdate (Intval ($date _arr[1)), Intval ($date _arr[2]), Intval ($date _arr[0])) {
- return false;
- }
- $year = Date (' Y ', Strtotime ($day));
- $weeks = Get_all_weeks ($year);
- The number of years and weeks that a certain day belongs to
- $real _year = date (' O ', Strtotime ($day));
- $week _num = intval (Date (' W ', Strtotime ($day)));
- if (!empty ($weeks [$real _year][$week _num][0])
- $begin _day = $weeks [$real _year][$week _num][0];
- if (!empty ($weeks [$real _year][$week _num][1])
- $end _day = $weeks [$real _year][$week _num][1];
- Year-over-week values are stored in the previous year
- if (Empty ($begin _day) | | Empty ($end _day)) {
- $year = Date (' Y ', Strtotime ($day))-1;
- $weeks = Get_all_weeks ($year);
- $real _year = date (' O ', Strtotime ($day));
- $week _num = intval (Date (' W ', Strtotime ($day)));
- if (!empty ($weeks [$real _year][$week _num][0])
- $begin _day = $weeks [$real _year][$week _num][0];
- if (!empty ($weeks [$real _year][$week _num][1])
- $end _day = $weeks [$real _year][$week _num][1];
- }
- $the _day = Array (
- ' Begin_day ' = $begin _day,
- ' End_day ' = $end _day
- );
- $the _day = Json_encode ($the _day);
- return $the _day;
- }
- Demo_ Call
- /*
- $day = ' 2014-01-01 ';
- if (Get_day_week ($day)) {
- Var_dump (Get_day_week ($day));
- }else{
- Echo ' date format error ';
- }
- */
Copy Code |