1. Determine the day of the week
- /**
- * Determine the day of the week
- * @param $date format ' YYYY-MM-DD ' format error returns false to correctly return a day from Monday to Sunday of the corresponding date
- */
- function Get_weekday ($date) {
- $date _arr = Explode ('-', trim ($date));
- if (!checkdate (Intval ($date _arr[1)), Intval ($date _arr[2]), Intval ($date _arr[0])) {
- return false;
- }
- Switch (date (' W ', Strtotime ($date))) {
- Case ' 0 ':
- $weekday = ' Day ';
- Break
- Case ' 1 ':
- $weekday = ' one ';
- Break
- Case ' 2 ':
- $weekday = ' two ';
- Break
- Case ' 3 ':
- $weekday = ' three ';
- Break
- Case ' 4 ':
- $weekday = ' four ';
- Break
- Case ' 5 ':
- $weekday = ' five ';
- Break
- Case ' 6 ':
- $weekday = ' VI ';
- Break
- Default
- return false;
- Break
- }
- return $weekday;
- }
- Demo_ Call
- $day = ' 2014-02-16 ';
- if (Get_weekday ($day)) {
- $test 1 = get_weekday ($day);
- Echo ' Week '. $test 1. '
';
- }else{
- Echo ' date error ';
- }
Copy Code2. Determine if the date format is correct
- /**
- * Determine if the date format is correct
- * Judging Format Yyyy-mm-dd | YYYY-MM-DD Hh:ii:ss
- * @param $tdate to determine the date
- * @param $dateformat to determine the date format "y-m-d" or "y-m-d h:i:s"
- */
- function Is_date ($tdate, $dateformat = "y-m-d") {
- $tdate = Trim ($tdate);
- cannot be converted to timestamp
- if (!is_numeric (Strtotime ($tdate))) return false;
- Determine if the date exists && month date format is y-m-d
- $tdate _date = Explode ("", $tdate);
- $tdate _time = Explode ("-", $tdate _date[0]);
- if (Isset ($tdate _time[0))
- $year = $tdate _time[0];
- Else
- return false;
- if (Isset ($tdate _time[1))
- $month = $tdate _time[1];
- Else
- return false;
- if (Isset ($tdate _time[2))
- $day = $tdate _time[2];
- Else
- return false;
- if (!checkdate ($month, $day, $year)) return false;
- Determines whether the date is in the specified format
- $tmpdate = Date ($dateformat, Strtotime ($tdate));
- if ($tmpdate = = $tdate)
- return true;
- Else
- return false;
- }
- /** Use demo */
- $tdate = ' 2014-02-16 11:25:33 ';
- $tdate = ' 2014-02-16 ';
- $tdate = ' 2014.02.16 ';
- $tdate = ' Asdqwe123123sadsad ';
- $dateformat = ' y-m-d ';
- $dateformat = "Y-m-d h:i:s";
- if (Is_date ($tdate, $dateformat)) {
- Echo ' true ';
- }else{
- echo ' false ';
- }
Copy Code3. Returns the date of all days between two dates
- /**
- * Returns the date of all days between two dates
- * Dependent Is_date ()
- * @param $tdate 1 $tdate 2 must be in ' y-m-d ' format
- * $tdate 1<= $tdate 2
- * Return string
- */
- function Getalldatebetdays ($tdate 1, $tdate 2) {
- $dateformat = ' y-m-d ';
- if (!is_date ($tdate 1, $dateformat)) return "date parameter 1 format error";
- if (!is_date ($tdate 2, $dateformat)) return "date parameter 2 format error";
- if ($tdate 1> $tdate 2) return "date parameter 2 must be greater than or equal to date parameter 1";
- $days = "'". $tdate 1. "'";
- while ($tdate 1< $tdate 2) {
- $days. = "'". Date ("Y-m-d", Strtotime ($tdate 1) +86400). "'";
- $tdate 1 = date ("y-m-d", Strtotime ($tdate 1) +86400);
- }
- return $days;
- }
- /** Use_demo */
- $tdate 1 = ' 2014-02-01 ';
- $tdate 1 = ' Asdqwe123123sadsad ';
- $tdate 2 = ' 2014-02-30 ';
- Echo getalldatebetdays ($tdate 1, $tdate 2);
Copy Code4. Determine if the month format is correct
- /**
- * Determine if the month format is correct
- * Judging Format yyyy-mm
- * @param $tmonth to determine the month
- * @param $monformat to determine the month date "Y-m"
- */
- function Is_month ($tmonth, $monformat = "Y-m") {
- $tmonth = Trim ($tmonth);
- cannot be converted to timestamp
- if (!is_numeric (Strtotime ($tmonth))) return false;
- Determines whether the month is a specified format
- $tmpmonth = Date ($monformat, Strtotime ($tmonth));
- if ($tmpmonth = = $tmonth)
- return true;
- Else
- return false;
- }
- /** Use_demo */
- $month = ' 02.16 ';
- $month = ' 2014-02 ';
- $monformat = "Y-m";
- if (Is_month ($month, $monformat))
- if (Is_month ($month))
- Echo ' true ';
- Else
- echo ' false ';
Copy Code5. Returns all months between two months
- /**
- * Returns all months between two months
- * @param $tmonth 1 $tmonth 2 must $tmonth 1< $tmonth 2
- * Return String
- */
- function Gatallmonbetmons ($tmonth 1, $tmonth 2) {
- $dateformat = "Y-m";
- if (!is_month ($tmonth 1, $dateformat)) return "month parameter 1 format error";
- if (!is_month ($tmonth 2, $dateformat)) return "Month parameter 2 format error";
- if ($tmonth 1> $tmonth 2) return "month parameter 2 must be greater than or equal to the month parameter 1";
- $months = "'". $tmonth 1. "'";
- while ($tmonth 1< $tmonth 2) {
- $months. = "'". Date ("Y-m", Strtotime ($tmonth 1. "-01". "+1 Month")). "'";
- $tmonth 1 = date ("Y-m", Strtotime ($tmonth 1.) "-01". "+1 Month"));
- }
- return $months;
- }
- /** Use_demo */
- $month 1 = ' 2013-01 ';
- $month 2 = ' 2014-02 ';
- Echo gatallmonbetmons ($month 1, $month 2);
Copy Code6. Date acquired relative to the current point in time
- /**
- * Relative to current point-in-time date acquisition
- * @param $needle "0": All Date Value "1": Yesterday "2": "3": the Day before Yesterday "4": Last month Today "5": Tomorrow
- * Last month today, if the number of days in the month is less than this month and the number of days is missing, the default is to return
- * Return corresponding date value JSON format
- */
- function Getneeddate ($needle) {
- $tdate = Date ("Y-m-d", Time ());
- $NeedDate = Array ();
- $NeedDate [1] = date ("Y-m-d", Time ()-86400);
- $NeedDate [2] = date ("Y-m-d", Time () -86400*2);
- $NeedDate [3] = date ("Y-m-d", Time () -86400*7);
- Days of last month
- $LMD _num = Date ("T", Strtotime (Date ("Y-m-d", Time ()). "-1 month"));
- Today is the first day of the month
- $tod _num = Date ("J", Time ());
- if ($tod _num<= $lmd _num) {
- $NeedDate [4] = date ("Y-m", Strtotime (Date ("Y-m-d", Time ()). "-1 month")). '-' . $tod _num;
- }else{
- $NeedDate [4] = date ("Y-m", Strtotime (Date ("Y-m-d", Time ()). "-1 month")). '-' . $LMD _num;
- }
- $NeedDate [5] = date ("Y-m-d", Time () +86400);
- Switch ($needle) {
- Case 0:
- Return Json_encode ($NeedDate);
- Break
- Case 1:
- return Json_encode ($NeedDate [1]);
- Break
- Case 2:
- return Json_encode ($NeedDate [2]);
- Break
- Case 3:
- return Json_encode ($NeedDate [3]);
- Break
- Case 4:
- return Json_encode ($NeedDate [4]);
- Break
- Default
- return false;
- Break
- }
- }
- /** Use_demo */
- Var_dump (Json_decode (getneeddate (0), true));
- Var_dump (Json_decode (Getneeddate (4), true));
Copy Code7. Leap Year judgment
- /**
- * Leap Year judgment
- * Leap Year calculation:
- * 1. Century can be divisible by 400
- * 2. Average year can be divisible by 4, but not divisible by 100
- * @param $year
- */
- function Isbissextile ($year) {
- $year = Intval (Trim ($year));
- $preg = "/^\d{4,}$/";
- if (!preg_match ($preg, $year))
- return false;
- if ($year%400==0) {
- return true;
- }elseif ($year%4==0 && $year%100!=0) {
- return true;
- }else{
- return false;
- }
- }
- /** Use demo */
- $year = ' 2012 ';
- if (Isbissextile ($year))
- Echo ' true ';
- Else
- echo ' false ';
Copy Code8. Number of days between two dates
- /**
- * Number of days between two dates
- * Dependent is_date
- * @param $tdate 1 $tdate 2 $tdate 1<= $tdate 2 dateformat: "Y-m-d"
- */
- function Getintervaldays ($tdate 1, $tdate 2) {
- $dateformat = ' y-m-d ';
- if (!is_date ($tdate 1, $dateformat)) return "date parameter 1 format error";
- if (!is_date ($tdate 2, $dateformat)) return "date parameter 2 format error";
- if ($tdate 1> $tdate 2) return "date parameter 2 must be greater than or equal to date parameter 1";
- $days = Ceil ((Strtotime ($tdate 2)-strtotime ($tdate 1))/86400);
- return $days;
- }
- /** Use demo */
- $tdate 1 = ' 2014-01-01 ';
- $tdate 2 = ' 2014-01-16 ';
- Echo getintervaldays ($tdate 1, $tdate 2);
Copy Code |