分类 PHP 下的文章

【PHP】获取上周、本周、上月、本月、本季度、上季度、上年、本年时间方法大全



/**
 * 根据类型获取时间格式
 * @param $time_type : 1年; 2季度;  3月; 4周; 5日;
 * @return time_start  开始时间
 * @return time_end    结束时间
 */
function sys_get_time_around($time_type)
{

    $now_start = 0;
    $now_end=0;
    $last_start= 0;
    $last_end = 0;
    $now_time = time();
    switch ($time_type) {
        case 'year':
            //今年
            $now_start = mktime(0,0,0,1,1,date('Y'));
            $now_end =  mktime(23,59,59,12,31,date('Y'));
            //去年
            $last_start = mktime(0,0,0,1,1,date('Y')-1);
            $last_end =  mktime(23,59,59,12,31,date('Y')-1);
            break;

        case 'quarter':
            $season = ceil((date('n'))/3)-1;//上季度是第几季度
            //本季度
            $now_season = ceil((date('n'))/3);//当月是第几季度
            $now_start = mktime(0, 0, 0,$now_season*3-3+1,1,date('Y'));
            $now_end = mktime(23,59,59,$now_season*3,date('t',mktime(0, 0 , 0,$now_season*3,1,date("Y"))),date('Y'));
            //上一季度
            $last_season = ceil((date('n'))/3)-1;//上季度是第几季度
            $last_start = mktime(0, 0, 0,$last_season*3-3+1,1,date('Y'));
            $last_end = mktime(23,59,59,$last_season*3,date('t',mktime(0, 0 , 0,$last_season*3,1,date("Y"))),date('Y'));
            break;

        case 'month':
            //本月
            $now_start = mktime(0,0,0,date('m'),1,date('Y'));
            $now_end = mktime(23,59,59,date('m'),date('t'),date('Y'));
            //上月
            $last_start = mktime(0,0,0,date('m')-1,1,date('Y'));
            $last_end = mktime(23,59,59,date('m'),0,date('Y'));
            break;

        case 'week':
            //mktime 是从周日开始算,结束时间是周六
            //本周
            // $now_start = mktime(0,0,0,date('m'),date('d')-date('w')+1,date('Y'));
            // $now_end   = mktime(23,59,59,date('m'),date('d')-date('w')+7,date('Y'));
            // //上周
            // $last_start = mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y'));
            // $last_end   = mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y'));

            //本周
            $now_start =   strtotime(date("Y-m-d 00:00:00",strtotime("this week Monday",$now_time )));
            $now_end   =   strtotime(date("Y-m-d 23:59:59",strtotime("this week Sunday",$now_time )));
            //上周
            $last_start =  strtotime(date("Y-m-d 00:00:00",strtotime("last week Monday",$now_time )));
            $last_end   =  strtotime(date("Y-m-d 23:59:59",strtotime("last week Sunday",$now_time )));
            break;
        case 'day':
            //今日
            $now_start = mktime(0,0,0,date('m'),date('d'),date('Y'));
            $now_end = mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
            //昨日
            $last_start = mktime(0,0,0,date('m'),date('d')-1,date('Y'));
            $last_end = mktime(23,59,59,date('m'),date('d')-1,date('Y'));

            break;
        default:
            # code...
            break;
    }

    $retult = [
        'time_type'=>$time_type,
        'now_start'=>date('Y-m-d H:i:s',$now_start),
        'now_end'=>date('Y-m-d H:i:s',$now_end),
        'last_start'=>date('Y-m-d H:i:s',$last_start),
        'last_end'=>date('Y-m-d H:i:s',$last_end),
    ];


    return $retult;
}

调用

   $a = sys_get_time_around('week');
   print_r($a);exit;

此处一定要注意,本周和上周的时候,mktime是从 周日到周六,避免被坑


【PHP】php实现的无限极分类


上代码

<?php

$rows = array(
  array(
    'id' => 1,
    'name' => 'dev',
    'parentid' => 0
  ),

  array(
    'id' => 2,
    'name' => 'php',
    'parentid' => 1
  ),

  array(
    'id' => 3,
    'name' => 'thinkphp',
    'parentid' => 2

  ),
  array(
    'id' => 4,
    'name' => 'life',
    'parentid' => 0
  ),

  array(
    'id' => 5,
    'name' => 'pdo',
    'parentid' => 2
  ),

  array(
    'id' => 6,
    'name' => 'pdo-mysql',
    'parentid' => 5
  ),

  array(
    'id' => 7,
    'name' => 'java',
    'parentid' => 1
  )
);


$tree=build_tree($arr,0);
print_r($tree);


function build_tree($rows,$root_id){
  $childs=findChild($rows,$root_id);
  if(empty($childs)){
    return null;
  }
  foreach ($childs as $k => $v){
    $rescurTree=build_tree($rows,$v['id']);
    if( null !=  $rescurTree){ 
      $childs[$k]['childs']=$rescurTree;
    }
  }
  return $childs;
}


function findChild($arr,$id){
  $childs=array();
   foreach ($arr as $k => $v){
     if($v['parentid']== $id){
       $childs[]=$v;
     }
  }
  return $childs;
}


?>


【PHP】判断一个字符串是否属于序列化后的数据


function is_serialized( $data ) {
     $data = trim( $data );
     if ( 'N;' == $data )
         return true;
     if ( !preg_match( '/^([adObis]):/', $data, $badions ) )
         return false;
     switch ( $badions[1] ) {
         case 'a' :
         case 'O' :
         case 's' :
             if ( preg_match( "/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data ) )
                 return true;
             break;
         case 'b' :
         case 'i' :
         case 'd' :
             if ( preg_match( "/^{$badions[1]}:[0-9.E-]+;\$/", $data ) )
                 return true;
             break;
     }
     return false;
 }