1. Determine the day of the week a certain day is
- /**
- * Determine what day of the week a certain day is
- * @param $date format 'YYYY-mm-dd' If the format is wrong, it will return false, if it is correct, it will return a day from Monday to Sunday in 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 = 'six';
- break;
- default:
- return false;
- break;
- }
- return $weekday;
- }
- //demo_call
- $day = '2014-02-16';
- if(get_weekday($day)){
- $test1 = get_weekday($day);
- echo 'week' . $test1 . '
';
- }else{
- echo 'date error';
- }
Copy code
2. Determine whether the date format is correct
- /**
- * Determine whether the date format is correct
- * Determine the format yyyy-mm-dd | yyyy-mm-dd hh:ii:ss
- * @param $tdate To 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;
- //Judge whether the date exists&& year, month and day The 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;
- //Judge 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 code
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 "The format of date parameter 1 is wrong";
- if( !is_date($tdate2,$dateformat) ) return "The format of date parameter 2 is wrong";
- 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 = '2014-02-01';
- //$tdate1 = 'asdqwe123123sadsad';
- $tdate2 = '2014-02-30';
- echo getAllDateBetDays( $tdate1,$tdate2);
Copy code 4. Determine whether the month format is correct - /**
- * Determine whether the month format is correct
- * Determine the format yyyy-mm
- * @param $tmonth To determine the month
- * @param $monformat The month date 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';
Copy code5. Return all months between two months - /**
- * Return all months between two months
- * @param $tmonth1 $tmonth2 must be $tmonth1<$tmonth2
- * return String
- */
- function gatAllMonBetMons($tmonth1,$tmonth2){
- $dateformat = "Y-m";
- if( !is_month($ tmonth1,$dateformat) ) return "The format of month parameter 1 is wrong";
- if( !is_month($tmonth2,$dateformat) ) return "The format of month parameter 2 is wrong";
- if( $tmonth1>$tmonth2 ) return "The month parameter 2 must be greater than or equal to month parameter 1";
- $months = "'" . $tmonth1 . "'";
- while( $tmonth1<$tmonth2 ){
- $months .= "'" . date("Y-m",strtotime ($tmonth1 . "-01" . "+1 month")) . "'";
- $tmonth1 = date("Y-m",strtotime($tmonth1 . "-01" . "+1 month"));
- }
- return $months;
- }
- /**use_demo*/
- $month1 = '2013-01';
- $month2 = '2014-02';
- echo gatAllMonBetMons($month1,$month2);
Copy code6. Get the date relative to the current time point - /**
- * Get the date relative to the current time point
- * @param $needle "0": All date values "1": Yesterday "2": The day before yesterday "3": Last week today "4": Last month today "5": Tomorrow
- * Today of the previous month, if the number of days in the previous month is less than the number of days in this month and the number of days is missing, the last day of the previous month will be returned by default
- * Return the corresponding date value in 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);
- //Number of days in the previous month
- $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));
Copy code7. Leap year judgment - /**
- * Leap year judgment
- * Leap year calculation:
- * 1. Century years can be divisible by 400
- * 2. Ordinary years can be divisible by 4, 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 = '2012';
- if( isBissextile($year) )
- echo 'true';
- else
- echo 'false';
Copy code8. The number of 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 has wrong format";
- 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 = '2014-01-01';
- $tdate2 = '2014-01-16';
- echo getIntervalDays($tdate1,$tdate2);
Copy code
|