PHP 基础 一些常用的函数 和方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/php_lzr/article/details/88073163

本人 收集了一些 灵活好用的方法 便于使用 有什么更好的方法 欢迎大家评论

获取一年中的全部月份日

$d0 = '2016-07-10';
$d1 = '2016-08-10';
$_time = range(strtotime($d0), strtotime($d1), 24*60*60);
$_time = array_map(create_function('$v', 'return date("Y-m-d", $v);'), $_time);
foreach ($_time as $key => $v) {
    $time[] =  strtotime($v);;
}

原生pdo链接数据库

header('Content-type:text/html;charset=utf-8');
set_time_limit(0);
//链接数据库
$dbms='mysql'; //数据库类型
$host=''; //数据库主机名
$dbName=''; //使用的数据库
$user=''; //数据库连接用户名
$pass='';  //对应的密码
$dsn="$dbms:host=$host;dbname=$dbName";
try {
    $pdo = new PDO($dsn, $user, $pass); //初始化一个PDO对象
    $pdo->query("SET NAMES utf8");
} catch (PDOException $e) {
    die ("Error!: " . $e->getMessage() . "<br/>");
}

批量插入数据sql

$sql = 'insert into history_event ';
$sql .='(`h_id`,`title`,`pic`,`year`,`month`,`day`,`desc`,`lunar`,`time`) values ';
foreach($result['result'] as $key => $val){
    $sql .= '(';
    foreach ($val as $v){
        $sql .= '"'.$v.'",';
    }
    $sql = rtrim( $sql, ',').'),';
}
$sql = rtrim($sql, ',').';';
$rs = $pdo->exec($sql);

}

下载

//设置为流的形式下载
//下载
function growingio_downlod($arr,$type,$time){
    if(!is_array($arr) || empty($arr)){
        $output = array(
            'status' => 2,
            'code' => 999,
            'error' => '不能为空',
        );
        exit(json_encode($output));
    }
    $num = 1;
    foreach ($arr as $v){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $v);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //不验证证书
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); //不验证证书
        curl_setopt($curl,CURLOPT_CONNECTTIMEOUT,10);
        $tmpInfo = curl_exec($curl);
        $download_dir = './GrowingIO/'.$time.'/'.$type.'/';
        if (!file_exists($download_dir)) {
            mkdir($download_dir, 0777, true);
        }
        file_put_contents($download_dir.$num++.'.csv',gzdecode($tmpInfo));
    }
}

file_get_contets 函数使用

file_put_contents("log.txt", "Hello world everyone.", FILE_APPEND);//追加写入

添加普通索引 单个字段

ALTER TABLE `file_info` ADD INDEX `file_url` (`file_url`) USING BTREE;

获取当前的周

 $week = date("w",$time);
//    $array = ["周日","周一","周二","周三","周四","周五","周六"];
//    return $array[$week];

日后必更新

猜你喜欢

转载自blog.csdn.net/php_lzr/article/details/88073163