关注前端开发
HTML5、CSS3、Javascript

php按指定日期范围获取周次,周次时间范围(支持跨年)

tidows阅读(1350)

获取周次

function getWeekNoToFirstDay($time){

    $iFirstWeekDay = date('w',strtotime(date("Y",$time).'-01-01'));


    if($iFirstWeekDay != 1){//指定年的1月1号不是星期一
        if($iFirstWeekDay == 0){//星期天

            if(date("Y-m-d",$time)==date("Y",$time).'-01-01'){
                //如果1号为周日,并且获取传入的日期也是 1号。

                $iFirstWeekDay = 7;

                $sWeekNo = strftime("%W",strtotime(date("Y-m-d",$time)));

            }else{

                $iFirstWeekDay = 7;

                $sWeekNo= strftime("%W",strtotime(date("Y-m-d",$time)))+1;

            }


        }else{

            $sWeekNo = strftime("%W",strtotime(date("Y-m-d",$time)))+1;

        }

        $iAdjustDay = (8 - $iFirstWeekDay)+($sWeekNo-1)*7;

        if($iAdjustDay<7){

            $iFirstWeekDay = $iFirstWeekDay-2;
            $dateFirstFullWeekBeginDate = strtotime("-{$iFirstWeekDay} day",strtotime((date("Y",$time)-1).'-12-31'));
        }else{

            $iAdjustDay = (8 - $iFirstWeekDay)+($sWeekNo-2)*7;
            $dateFirstFullWeekBeginDate = strtotime("+{$iAdjustDay} day",strtotime(date("Y",$time).'-01-01'));

        }

    }else{


        if(date("Y-m-d",$time)==date("Y",$time).'-01-01'){
            //如果1号为周日,并且获取传入的日期也是 1号。

            $sWeekNo = strftime("%W",strtotime(date("Y-m-d",$time)));

        }else{

            $sWeekNo= strftime("%W",strtotime(date("Y-m-d",$time)))+1;

        }


        $iAdjustDay = ($sWeekNo-1)*7;
        $dateFirstFullWeekBeginDate = strtotime("+{$iAdjustDay} day",strtotime(date("Y",$time).'-01-01'));
    }
    return $dateFirstFullWeekBeginDate;
}

 

获取周次范围:

function getWeekNoToDateRange($time,$is_timestamp=false){

    $dateTemp=getWeekNoToFirstDay($time);

    if($is_timestamp){

        $sDateRange['start_week_day'] = $dateTemp;
        $sDateRange['end_week_day'] = strtotime("+7 day",$dateTemp);

    }else{

        $sDateRange['start_week_day'] = date('Y-m-d H:i:s',$dateTemp);
        $sDateRange['end_week_day'] = date('Y-m-d H:i:s',strtotime("+7 day",$dateTemp));

    }

    return $sDateRange;
}

 

获取指定日期范围一共有多少周

function getWeekNoToTotalWeeks($start_time,$end_time){

    if(!$start_time or !$end_time ){

        return null;
    }
    $time_from_yearweek=getWeekNoToFirstDay($start_time);
    $time_end_yearweek=getWeekNoToFirstDay($end_time);

    $total_weeks=($time_end_yearweek-$time_from_yearweek)/86400/7;
    if($total_weeks >= 0){
        $total_weeks++;
    }
    return $total_weeks;
}

 

使用:

时间:2023-01-01 00:00:00  周日

print_r(getWeekNoToDateRange(1672502400));

Array ( [start_week_day] => 2022-12-26 00:00:00 [end_week_day] => 2023-01-02 00:00:00 )

时间:2023-01-02 00:00:00 周一

print_r(getWeekNoToDateRange(1672588800));

Array ( [start_week_day] => 2023-01-02 00:00:00 [end_week_day] => 2023-01-09 00:00:00 )

取时间:2022-12-31 00:00:00

print_r(getWeekNoToDateRange(1672416000));

Array ( [start_week_day] => 2022-12-26 00:00:00 [end_week_day] => 2023-01-02 00:00:00 )

 

 

 

欢迎指正!

参考:https://blog.csdn.net/lanyd/article/details/6232171

TP5中使用 FIND_IN_SET进行查询

tidows阅读(5070)

有的时候我们的数据存入数据的时候格式是这样的  1,2,3,4

当使用的时候 我们需要查询出  某一个数值在 这个字段里面,这种情况就需要用到 FIND_IN_SET

在thinkphp5 中 我们可以使用 FIND_IN_SET 来进行查询

$where[] = [‘exp’,’FIND_IN_SET(2,category_id)’];

db()->where($where)->select();

 

5.0.17后版本写法

 

$where=[]=>[‘exp’,Db::raw(“FIND_IN_SET(2,category_id)”)]]

 

 

tp3里面的写法

$where[‘_string’] = ‘FIND_IN_SET(2,category_id)’;

 

thinkphp分页参数带空格被转换成加号“+”的问题

tidows阅读(4940)

thinkphp分页中参数如果带有空格,空格会被编码成“+”,那么有些分页数据就没法正常读取。

处理方法:找到Page.class.php 分页文件类,

private function url($page){
  return str_replace('[PAGE]', $page, urldecode($this->url));
}

thinkphp 结合phpexcel实现excel导入

tidows阅读(11115)

控制器文件:

class ExcelAction extends Action {
	public function __construct()
	{
		import('ORG.Util.ExcelToArrary');//导入excelToArray类
	}

	public function index()
	{
		$this->display();
	}
	public function add()
	{	
		$tmp_file = $_FILES ['file_stu'] ['tmp_name'];
		$file_types = explode ( ".", $_FILES ['file_stu'] ['name'] );
		$file_type = $file_types [count ( $file_types ) - 1];

		 /*判别是不是.xls文件,判别是不是excel文件*/
		 if (strtolower ( $file_type ) != "xlsx" && strtolower ( $file_type ) != "xls")              
		 {
			  $this->error ( '不是Excel文件,重新上传' );
		 }

		 /*设置上传路径*/
		 $savePath = C('UPLOAD_DIR');

		 /*以时间来命名上传的文件*/
		 $str = date ( 'Ymdhis' ); 
		 $file_name = $str . "." . $file_type;

		 /*是否上传成功*/
		 if (! copy ( $tmp_file, $savePath . $file_name )) 
		  {
			  $this->error ( '上传失败' );
		  }
		$ExcelToArrary=new ExcelToArrary();//实例化
		$res=$ExcelToArrary->read(C('UPLOAD_DIR').$file_name,"UTF-8",$file_type);//传参,判断office2007还是office2003

		foreach ( $res as $k => $v ) //循环excel表
		   {
			   $k=$k-1;//addAll方法要求数组必须有0索引
			   $data[$k]['name1'] = $v [0];//创建二维数组
			   $data[$k]['name2'] = $v [1];
			   $data[$k]['name3'] = $v [2];		
			   $data[$k]['name4'] = $v [3];
		  }
		  $kucun=M('kucun');//M方法
		  $result=$kucun->addAll($data);
		  if(! $result)
		  {
			  $this->error('导入数据库失败');
			  exit();
		  }
		  else
		  {
			  $this->success ( '导入成功' );	
		  }
	}

定义Excel转换数组类

class ExcelToArrary {
  public function __construct() {
		Vendor("Excel.PHPExcel");//引入phpexcel类(注意你自己的路径)
		Vendor("Excel.PHPExcel.IOFactory"); 	
  }
  public function read($filename,$encode,$file_type){
	        if(strtolower ( $file_type )=='xls')//判断excel表类型为2003还是2007
			{
				Vendor("Excel.PHPExcel.Reader.Excel5"); 
				$objReader = PHPExcel_IOFactory::createReader('Excel5');
			}elseif(strtolower ( $file_type )=='xlsx')
			{
				Vendor("Excel.PHPExcel.Reader.Excel2007"); 
				$objReader = PHPExcel_IOFactory::createReader('Excel2007');
			}
			$objReader->setReadDataOnly(true);
			$objPHPExcel = $objReader->load($filename);
			$objWorksheet = $objPHPExcel->getActiveSheet();
			$highestRow = $objWorksheet->getHighestRow();
			$highestColumn = $objWorksheet->getHighestColumn();
			$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
			$excelData = array();
			for ($row = 1; $row <= $highestRow; $row++) {
				for ($col = 0; $col < $highestColumnIndex; $col++) {
					$excelData[$row][] =(string)$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
					}
			}
			return $excelData;
	  }

前台导入页面:

导入Excel表:

原文链接:http://blog.csdn.net/sui13725202/article/details/9285501

Thinkphp 模版中的日期减法运算

tidows阅读(5196)

首先在 Common目录中建立common.php 文件 ,ThinkPhp会自动加载这个common.php文件;在文件中写入

<?php
function xiangjian($datetime){
$result=(strtotime($datetime)-strtotime(date(‘Y-m-d’,time())))/3600/24;
return $result;
}
?>

我用的是数据库读取出来的时间数据和当前的时间进行相减;

然后在模版中进行调用:

<{$vo.webendtime|xiangjian=###}>天

//调用xiangjian函数 把$vo.webendtime的值传入到函数中。