~~~
/**
* 获取$type类型下过去$day天/周/月的日期
* @param string $type day/week/month
* @param int $day
* @return array
*/
function get_last_date($day, $type = 'day') {
$now_date = date('Y-m-d');
(int) $day;
for ($i = 1; $i <= $day; $i++) {
$date = date('Y-m-d', strtotime("$now_date - $i $type"));
if ($type == 'day') {
$rdate['start_day'] = strtotime("$date 00:00:00");
$rdate['end_day'] = strtotime("$date 23:59:59");
$data[] = $rdate;
} else {
$rdate = $this->get_date_belong($date, $type);
$data[] = $rdate;
}
}
$data = array_reverse($data);
return $data;
}
/**
* 获取日期所在周/月的开始日期跟结束日期
* @param type $date
* @param type $type
* @return array
*/
function get_date_belong($date, $type) {
if ($type == 'week') {
$idx = strftime("%u", strtotime($date));
$mon_idx = $idx - 1;
$sun_idx = $idx - 7;
$data = array(
'start_day' => strtotime("$date 00:00:00") - $mon_idx * 86400,
'end_day' => strtotime("$date 23:59:59") - $sun_idx * 86400,
);
} else if ($type == 'month') {
$ret = array();
$timestamp = strtotime($date);
$mdays = date('t', $timestamp);
$data = array(
'start_day' => strtotime(date('Y-m-01 00:00:00', $timestamp)),
'end_day' => strtotime(date('Y-m-' . $mdays . " 23:59:59", $timestamp))
);
}
return $data;
}
/**
* 二维数组根据字段进行排序
* @params array $array 需要排序的数组
* @params string $field 排序的字段
* @params string $sort 排序顺序标志 SORT_DESC 降序;SORT_ASC 升序
*/
function arraySequence($array, $field, $sort = 'SORT_DESC')
{
$arrSort = array();
foreach ($array as $uniqid => $row) {
foreach ($row as $key => $value) {
$arrSort[$key][$uniqid] = $value;
}
}
array_multisort($arrSort[$field], constant($sort), $array);
return $array;
}
/*
* 获取ip地址
* @return string
*/
function get_ip() {
if (getenv("HTTP_CLIENT_IP"))
$ip = getenv("HTTP_CLIENT_IP");
else if (getenv("HTTP_X_FORWARDED_FOR"))
$ip = getenv("HTTP_X_FORWARDED_FOR");
else if (getenv("REMOTE_ADDR"))
$ip = getenv("REMOTE_ADDR");
else
$ip = "Unknow";
return $ip;
}
/*
* curl post 提交数据
*/
function curl($url, $data) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_TIMEOUT_MS, 300);//设置超时时间
//curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //表单提交方式
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
function curl_get($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
/*
* 保存远程文件到本地
* *@param $url string 远程文件路径
* @param $save_dir string 本地保存绝对地址
* @param $filename string 保存的文件名
* @param $type int
* @return string
*/
function get_image($url, $save_dir = '', $filename = '', $type = 0) {
if (trim($url) == '') {
return array('file_name' => '', 'save_path' => '', 'error' => 1);
}
if (trim($save_dir) == '') {
$save_dir = './';
}
if (trim($filename) == '') {//保存文件名
$ext = strrchr($url, '.');
if ($ext != '.gif' && $ext != '.jpg') {
return array('file_name' => '', 'save_path' => '', 'error' => 3);
}
$filename = time() . $ext;
}
if (0 !== strrpos($save_dir, '/')) {
$save_dir.='/';
}
//创建保存目录
if (!file_exists($save_dir) && !mkdir($save_dir, 0777, true)) {
return array('file_name' => '', 'save_path' => '', 'error' => 5);
}
//获取远程文件所采用的方法
if ($type) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$img = curl_exec($ch);
curl_close($ch);
} else {
ob_start();
readfile($url);
$img = ob_get_contents();
ob_end_clean();
}
//$size=strlen($img);
//文件大小
$fp2 = @fopen($save_dir . $filename, 'a');
fwrite($fp2, $img);
fclose($fp2);
unset($img, $url);
return array('file_name' => $filename, 'save_path' => $save_dir . $filename, 'error' => 0);
}
/**
* 上传文件
* @param $filename string 控件名
* @param $path string 文件路径
* @param $filetype array 文件限制的类型
* @param $maxSize int 上传文件限制的大小 默认为 2M
* @return string
*/
function upload_file($filename, $path, $filetype = NULL, $maxSize = 2097152) {
if (!$_FILES[$filename]["name"]) {
return false;
}
if (!in_array($_FILES[$filename]["type"], $filetype)) {
return false;
}
if ($_FILES[$filename]["error"] > 0) {
return false;
}
if ($_FILES[$filename]["size"] > $maxSize) {
return false;
}
//如果路径不存在,则创建
if (!file_exists($path)) {
mkdir($path, 0777, true);
}
$time = $_SERVER['REQUEST_TIME'];
$fname = md5(date('YmdHis_', $time) . rand(1000, 9999)) . strrchr($_FILES[$filename]["name"], ".");
$res = move_uploaded_file($_FILES[$filename]["tmp_name"], $path . $fname);
if (!$res) {
return false;
}
return str_replace(FILES_PATH, FILES_DOMAIN, $path . $fname);
}
/*
* AES加密函数
* @param string $plain_text 需要加密的数据
* @param string $key 加密的KEY
* @return string
*/
function aes_encode($plain_text, $key = SIGN_KEY) {
return base64_encode(openssl_encrypt($plain_text, "aes-256-cbc", $key, true, str_repeat(chr(0), 16)));
}
/*
* AES解密函数
* @param string $base64_text 需要解密的数据
* @param string $key 解密的KEY
* @return string
*/
function aes_decode($base64_text, $key = SIGN_KEY) {
return openssl_decrypt(base64_decode($base64_text), "aes-256-cbc", $key, true, str_repeat(chr(0), 16));
}
/**
* 十进制数组转换成字符串
* @param array $data
* @return string
*/
function get_pack($data) {
$pack = '';
foreach ($data as $d) {
$pack .= pack("C*", $d);
}
return $pack;
}
/**
* 字符串转换成十进制数组
* @param string $str
* @return array
*/
function get_unpack($str) {
return unpack("C*", $str);
}
~~~