|
1. determine the day of the week
- /**
- * Determine the day of the week.
- * @ Param $ date: 'yyyy-mm-DD'. If an error occurs, false is returned. If yes, a day from Monday to Sunday is returned.
- */
- 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 = '1 ';
- Break;
- Case '2 ':
- $ Weekday = '2 ';
- Break;
- Case '3 ':
- $ Weekday = '3 ';
- Break;
- Case '4 ':
- $ Weekday = '4 ';
- Break;
- Case '5 ':
- $ Weekday = '5 ';
- Break;
- Case '6 ':
- $ Weekday = '6 ';
- Break;
- Default:
- Return false;
- Break;
- }
- Return $ weekday;
- }
- // Demo _ call
- $ Day = '2017-02-16 ';
- If (get_weekday ($ day )){
- $ Test1 = get_weekday ($ day );
- Echo 'week'. $ test1 .'
';
- } Else {
- Echo 'date error ';
- }
2. determine whether the date format is correct
- /**
- * Determine whether the date format is correct
- * Format: yyyy-mm-dd | yyyy-mm-dd hh: ii: ss
- * @ Param $ tdate: determine the date
- * @ Param $ dateformat the date format to be determined: "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 a timestamp
- If (! Is_numeric (strtotime ($ tdate) return false;
- // Determine whether the date exists & the format of year, month, and Day 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;
- // Determine whether the date is in the specified format
- $ Tmpdate = date ($ dateformat, strtotime ($ tdate ));
- If ($ tmpdate = $ tdate)
- Return true;
- Else
- Return false;
- }
- /** Use demo */
- $ Tdate = '2017-02-16 11:25:33 ';
- // $ Tdate = '2017-02-16 ';
- // $ Tdate = '2017. 123 ';
- // $ Tdate = 'asdqwe123123sadsad ';
- $ Dateformat = 'Y-m-D ';
- // $ Dateformat = "Y-m-d H: I: s ";
- If (is_date ($ tdate, $ dateformat )){
- Echo 'true ';
- } Else {
- Echo 'false ';
- }
3. return all days between two dates
- /**
- * Returns all days between two dates.
- * Depends on is_date ()
- * @ Param $ tdate1 $ tdate2 must be in 'Y-m-d' format
- * $ Tdate1 <= $ tdate2
- * Return string
- */
- Function getAllDateBetDays ($ tdate1, $ tdate2 ){
- $ Dateformat = 'Y-m-D ';
- If (! Is_date ($ tdate1, $ dateformat) return "date parameter 1 format error ";
- If (! Is_date ($ tdate2, $ dateformat) return "date parameter 2 format error ";
- If ($ tdate1> $ tdate2) return "date parameter 2 must be greater than or equal to date parameter 1 ";
- $ Days = "'". $ tdate1 ."'";
- While ($ tdate1 <$ tdate2 ){
- $ Days. = "'". date ("Y-m-d", strtotime ($ tdate1) + 86400 )."'";
- $ Tdate1 = date ("Y-m-d", strtotime ($ tdate1) + 86400 );
- }
- Return $ days;
- }
- /** Use_demo */
- $ Tdate1 = '2017-02-01 ';
- // $ Tdate1 = 'asdqwe123123sadsad ';
- $ Tdate2 = '2017-02-30 ';
- Echo getAllDateBetDays ($ tdate1, $ tdate2 );
4. determine whether the month format is correct
- /**
- * Determine whether the month format is correct
- * The judgment format is yyyy-mm.
- * @ Param $ tmonth determine the month
- * @ Param $ monformat the date of the month to be determined "Y-m"
- */
- Function is_month ($ tmonth, $ monformat = "Y-m "){
- $ Tmonth = trim ($ tmonth );
- // Cannot be converted to a timestamp
- If (! Is_numeric (strtotime ($ tmonth) return false;
- // Determine whether the month is in the 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 ';
5. return all months between two months.
- /**
- * Returns all months between two months.
- * @ Param $ tmonth1 $ tmon2$ $ tmonth1 <$ tmon22.
- * Return String
- */
- Function gatAllMonBetMons ($ tmonth1, $ tmon2 ){
- $ Dateformat = "Y-m ";
- If (! Is_month ($ tmonth1, $ dateformat) return "month parameter 1 format error ";
- If (! Is_month ($ tmon2, $ dateformat) return "month parameter 2 format error ";
- If ($ tmonth1> $ tmon2) return "the month parameter 2 must be greater than or equal to the month parameter 1 ";
- $ Months = "'". $ tmonth1 ."'";
- While ($ tmonth1 <$ tmon2 ){
- $ Months. = "'". date ("Y-m", strtotime ($ tmonth1. "-01". "+ 1 month "))."'";
- $ Tmonth1 = date ("Y-m", strtotime ($ tmonth1. "-01". "+ 1 month "));
- }
- Return $ months;
- }
- /** Use_demo */
- $ Month1 = '2017-01 ';
- $ Mon22. = '2014-02 ';
- Echo gatAllMonBetMons ($ month1, $ mon22 );
6. get the date from the current time point
- /**
- * Get relative to the current time point date
- * @ Param $ needle "0": all date values "1": Yesterday "2": The day before yesterday "3": Last week today "4": Last month today "5": tomorrow
- * If the number of days in the previous month is less than the number of days in the current month and the number of days is missing, the last day of the previous month is returned by default.
- * Return the json format of the corresponding date value
- */
- 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 );
- // Last month's days
- $ Lmd_num = date ("t", strtotime (date ("Y-m-d", time (). "-1 month "));
- // Today is the 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 ));
7. leap year judgment
- /**
- * Leap year judgment
- * Leap year calculation:
- * 1. the century can be divisible by 400
- * 2. an ordinary year can be divisible by four, but not 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 = '000000 ';
- If (isBissextile ($ year ))
- Echo 'true ';
- Else
- Echo 'false ';
8. days between two dates
- /**
- * Number of days between two dates
- * Depends on is_date
- * @ Param $ tdate1 $ tdate2 $ tdate1 <= $ tdate2 dateformat: "Y-m-d"
- */
- Function getIntervalDays ($ tdate1, $ tdate2 ){
- $ Dateformat = 'Y-m-D ';
- If (! Is_date ($ tdate1, $ dateformat) return "date parameter 1 format error ";
- If (! Is_date ($ tdate2, $ dateformat) return "date parameter 2 format error ";
- If ($ tdate1> $ tdate2) return "date parameter 2 must be greater than or equal to date parameter 1 ";
- $ Days = ceil (strtotime ($ tdate2)-strtotime ($ tdate1)/86400 );
- Return $ days;
- }
- /** Use demo */
- $ Tdate1 = '2017-01-01 ';
- $ Tdate2 = '2017-01-16 ';
- Echo getIntervalDays ($ tdate1, $ tdate2 );
|