/**
* 转换字节大小
* @param [number] $size
* @param integer $digits
* @return [string]
*/
function transByte($size, $digits=2) {
$units = ['B','KB','MB','GB','TB','PB'];
$base = 1024;
$i = floor(log($size,$base));
return round($size/pow($base,$i),$digits).$units[$i];
}
/**
* 转换字节大小
* @param [number] $size
* @return [string]
*/
function format_file_size($size){
$units = ['B','KB','MB','GB','TB','PB'];
for ($i = 0; $size >= 1024 && $i < 4; $i++){
$size /= 1024;
}
return round($size, 2).$units[$i];
/**
* 读取目录大小
* @param [string] $path
* @return [number]
*/
function getDirSize($path) {
global $sum;
$handle = opendir($path);
while (($item=@readdir($handle))!==false) {
$filePath = $path.'/'.$item;
if($item!='.'&&$item!='..'){
if(is_file($filePath)) {
$sum += filesize($filePath);
}
if(is_dir($filePath)) {
$func = __FUNCTION__;
$func($filePath);
}
}
}
close($handle);
return $sum;
}
/**
* 获取某周开始结束日期
* @param $year 哪一年
* @param int $week 第几周
* @return mixed
*/
function getWeekday($year, $week = 1)
{
$year_start = mktime(0, 0, 0, 1, 1, $year);
$year_end = mktime(0, 0, 0, 12, 31, $year);
// 判断第一天是否为第一周的开始
if (intval(date('W', $year_start)) === 1) {
$start = $year_start;//把第一天做为第一周的开始
} else {
$week++;
$start = strtotime('+1 monday', $year_start);//把第一个周一作为开始
}
// 第几周的开始时间
if ($week === 1) {
$weekday['start'] = $start;
} else {
$weekday['start'] = strtotime('+' . ($week - 0) . ' monday', $start);
}
// 第几周的结束时间
$weekday['end'] = strtotime('+1 sunday', $weekday['start']);
if (date('Y', $weekday['end']) != $year) {
$weekday['end'] = $year_end;
}
return $weekday;
}
/**
* 获取某月开始结束日期
* @param $date
* @return mixed
*/
function getMonthRange($date)
{
$timestamp = strtotime($date);
$monthFirstDay = date('Y-m-1 00:00:00', $timestamp);//当月第一天
$monthDay['start'] = $monthFirstDay;
$countDays = date('t', $timestamp);//当月天数
$monthDay['end'] = date('Y-m-' . $countDays . ' 23:59:59', $timestamp);
return $monthDay;
}
/**
* 模拟请求 http
* @param $url
* @param int $httpCode
* @return mixed
*/
function curl_get($url, &$httpCode = 0)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//不做证书校验,部署在linux环境下请改为true
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
$files_contents = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $files_contents;
}
/**
* 将xml转换为数组
* @param $xml 需要转换的xml
* @return mixed
*/
function xml_to_array($xml) {
$ob = simplexml_load_string($xml);
$json = json_encode($ob);
$array = json_decode($json, true);
return $array;
}
/**
* 将数组转换成xml
* @param $data 需要转换的数组
* @return string
*/
function data_to_xml($data) {
if (is_object($data)) {
$data = get_object_vars($data);
}
$xml = '';
foreach ($data as $key => $val) {
if (is_null($val)) {
$xml .= "<$key/>\n";
} else {
if (!is_numeric($key)) {
$xml .= "<$key>";
}
$xml .= (is_array($val) || is_object($val)) ? self::data_to_xml($val) : $val;
if (!is_numeric($key)) {
$xml .= "$key>";
}
}
}
return $xml;
}
/**
* 接收xml数据并转换成数组
* @return array
*适合用在微信公众号开发
*/
function getRequestBean() {
$bean = simplexml_load_string(file_get_contents('php://input')); // simplexml_load_string() 函数把 XML 字符串载入对象中。如果失败,则返回 false。
$request = array();
foreach ($bean as $key => $value) {
$request [( string )$key] = ( string )$value;
}
return $request;
}
function togbk($val) {
return mb_convert_encoding($val,'gbk','utf-8');
}
function toutf8($val) {
return mb_convert_encoding($val, 'utf-8', 'gbk');
}
//或者使用iconv函数
**
* 获取随机字符串
* @param $length
* @return string
*/
function getRandChar($length)
{
$str = str_shuffle(join('',array_rand(array_flip(array_merge(range('a','z'),range('A','Z'),range(0,9))),$length)));
return $str;
}
date('W',strtotime('2015-10-25'));