自动创建工作副本简单版

<?php

/**
 * 自动创建工作总结文件简单版
 */

/**
 *
 * @author fantom
 * @time  2011-02-27
 */
class createDir {

    private $_year; //年份
    private $_month; //月份
    private $_exclude; //排除的天数
    private $_createDir; //目录路径
    private $_postfix; //后缀名

    public function __construct($createDir, $year, $month, $postfix = 'txt', $exclude = array(6, 0)) {
        $this->_createDir = $createDir;
        $this->_year = $year;
        $this->_month = $month;
        $this->_postfix = $postfix;
        $this->_exclude = $exclude;
    }

    public function createDir() {
        $dir = $this->doCreateDir($this->_createDir . DIRECTORY_SEPARATOR . $this->_year . DIRECTORY_SEPARATOR . $this->_month);
        $i = 1;
        $resultArr = array();
        $monthArr = array(4, 6, 9, 11);
        for ($i; $i <= 31; $i++) {
            //二月处理
            if ($this->_month == 2 && $i > 28) {
                if ($this->_year % 4 == 0) {
                    if ($i > 29) {
                        break;
                    }
                } else {
                    break;
                }
            }

            //排除31天
            if (in_array($this->_month, $monthArr) && $i == 31) {
                break;
            }

            //排除的星期
            $weekDay = date('w', mktime(0, 0, 0, $this->_month, $i, $this->_year));
            if (in_array($weekDay, $this->_exclude)) {
                continue;
            }
            $createdir = $dir . DIRECTORY_SEPARATOR . $this->_month . '-' . $i . '.' . $this->_postfix;
            $resultArr[] = file_put_contents($createdir, $this->doDefault($i));
        }
        return $resultArr;
    }

    /**
     * 多层创建目录方法
     * $dir 目录结构,先分拆,看是否存在
     * author fantom
     * time 2011-10-10
     */
    private function doCreateDir($dir) {
        $dirArr = explode(DIRECTORY_SEPARATOR, $dir); //把多级目录分别放到数组中
        $dirCount = count($dirArr);
        $path = null;
        for ($i = 0; $i < $dirCount; $i++) {
            $path .= $dirArr[$i] . DIRECTORY_SEPARATOR;
            if (!is_dir($path)) {
                mkdir($path, 0777);
            }
        }
        return $dir;
    }

    private function doDefault($day) {
        return $this->_month . '-' . $day . '工作总结:';
    }

}

$createDemo = new createDir('h:' . DIRECTORY_SEPARATOR . 'demo', date('Y'), 2);
$createDemo->createDir();

你可能感兴趣的:(工作)