日期时间
1、计算时间差
/**
* @title 计算时间差
* @author lyj [author] [2018-06-07]
* @param [type] $datetimeStr1 [日期时间字符串1]
* @param [type] $datetimeStr2 [日期时间字符串2]
* @return [type] [返回秒数]
*/
function getDateTimeDifference($datetimeStr1,$datetimeStr2){
return strtotime($datetimeStr1)-strtotime($datetimeStr2);
}
2、时间戳 转 日期时间字符串
/**
* @title 时间戳 转 日期时间字符串
* @author lyj [author] [2018-06-07]
* @param [type] $timeStamp [时间戳]
* @param string $pattern [日期时间格式 Y-m-d H:i:s]
* @return [type] [返回 日期时间字符串]
*/
function getDateTimeStr($timeStamp = null,$pattern = 'Y-m-d H:i:s'){
if($timeStamp == null || !is_int($timeStamp)){
return date($pattern);
} else {
return date($pattern,$timeStamp);
}
}
3、根据需要返回对应时间戳
/**
* @title 根据需要返回对应时间戳
* @author lyj [author] [2018-06-04]
* @param string $config [description]
* @return [type] [description]
*/
function getTimestamp($config = '')
{
switch ($config) {
case '1':
// 返回 明天 的Unix时间戳
return strtotime("+1 day");
break;
case '2':
// 返回 昨天 的Unix时间戳
return strtotime("-1 day");
break;
case '3':
// 返回 下月 的Unix时间戳
return strtotime("+1 month");
break;
case '4':
// 返回 上月 的Unix时间戳
return strtotime("-1 month");
break;
case '5':
// 返回 明年 的Unix时间戳
return strtotime("+1 year");
break;
case '6':
// 返回 上月 的Unix时间戳
return strtotime("-1 year");
break;
case '7':
// 返回 下星期 的Unix时间戳
return strtotime("+1 week");
break;
case '8':
// 返回 上星期 的Unix时间戳
return strtotime("+1 week");
break;
case '9':
// 返回 本月第一天 的Unix时间戳
return strtotime(date("Y-m-01 H:i:s"));
break;
case '10':
// 返回 本月最后一天 的Unix时间戳
return strtotime(date("Y-m-t H:i:s"));
break;
case '11':
// 返回 本周第一天 的Unix时间戳
return strtotime("this week");
break;
case '12':
// 返回 本周最后一天 的Unix时间戳
return strtotime("last day this week");
break;
case '13':
// 返回 今天开始 的Unix时间戳
return strtotime(date("Y-m-d"));
break;
default:
// 返回 当前 的Unix时间戳
return time();
break;
}
}
其他
1、ajax返回
/**
* @title ajax返回
* @author lyj [author] [2018-06-04]
* @param [type] $info [提示信息]
* @param integer $status [状态]
* @param [type] $data [数据]
* @return [type] [返回json数据]
*/
function ajaxReturn($info,$status = 0,$data = null)
{
$result = array();
$result['status'] = $status;
$result['info'] = $info;
$result['data'] = $data;
// 返回JSON数据格式到客户端 包含状态信息
header("Content-Type:text/html; charset=utf-8");
exit(json_encode($result));
}
2、图片设置文字水印
/**
* @title 图片设置文字水印
* @author lyj [author] [2018-05-30]
* @param [type] $pic [图片路径]
* @param [type] $word [文字]
* @param [type] $size [文字大小]
* @param [type] $x [文字x坐标]
* @param [type] $y [文字y坐标]
* @param string $newpic [新图片路径]
* @param integer $print [是否打印]
* @return [type] [打印或生成图片]
*
* 使用方法 imgSetWord('123.png','hhhhhhhaaaahahahaha',20,0,100,'aa.png',0);
*/
function imgSetWord($pic,$word,$size,$x,$y,$newpic = '',$print = 0)
{
if($newpic == ''){
$newpic = $pic;
}
// 图片对象
$dst = imagecreatefromstring(file_get_contents($pic));
// 字体
$font = './yahei.ttf';
// 字体颜色
$color = imagecolorallocate($dst, 255, 213, 95);
imagefttext($dst, $size, 0, $x, $y, $color, $font, $word);
// 图片宽高等信息
list($dst_w, $dst_h, $dst_type) = getimagesize($pic);
if($print == 0){
switch ($dst_type) {
case 1://GIF
imagegif($dst,$newpic);
break;
case 2://JPG
imagejpeg($dst,$newpic);
break;
case 3://PNG
imagepng($dst,$newpic);
break;
default:
break;
}
return $newpic;
} else {
switch ($dst_type) {
case 1://GIF
header('Content-Type: image/gif');
imagegif($dst);
break;
case 2://JPG
header('Content-Type: image/jpeg');
imagejpeg($dst);
break;
case 3://PNG
header('Content-Type: image/png');
imagepng($dst);
break;
default:
break;
}
}
}
3、判断是否json
/**
* @title 判断是否json
* @author lyj [author] [2018-05-16]
* @param [type] $str [需要检查的json字符串]
* @return boolean [description]
*/
function is_json($str)
{
json_decode($str);
return (json_last_error() == JSON_ERROR_NONE);
}
国际单位制
1B = 8 bit ;(byte,字节)
1KB = 1000B = 10^3B;(kilobyte,千字节)
1MB = 1000KB = 10^6B;(Megabyte,兆字节,百万字节,简称“兆”)
1GB = 1000MB = 10^9B;(Gigabyte,吉字节,十亿字节,又称“千兆”)
1TB = 1000GB = 10^12B;(Terabyte,万亿字节,太字节)
1PB = 1000TB = 10^15B;(Petabyte,千万亿字节,拍字节)
1EB = 1000PB = 10^18B;(Exabyte,百亿亿字节,艾字节)
1ZB = 1000EB = 10^21B;(Zettabyte,十万亿亿字节,泽字节)
1YB = 1000ZB = 10^24B;(Yottabyte,一亿亿亿字节,尧字节)
1BB = 1000YB = 10^27B;(Brontobyte,一千亿亿亿字节)
1NB = 1000BB = 10^30B;(NonaByte,一百万亿亿亿字节)
1DB = 1000NB = 10^33B;(DoggaByte,十亿亿亿亿字节)
数据存储是以10进制表示,数据传输是以2进制表示的,所以1KB不等于1000B。
1KiB = 1024B = 2^10 B;(Kibibyte,千字节) 1MiB = 1024KB = 2^20 B;(Mebibyte,兆字节,百万字节,简称“兆”) 1GiB = 1024MB = 2^30 B;(Gibibyte,吉字节,十亿字节,又称“千兆”) 1TiB = 1024GB = 2^40 B;(Tebibyte,万亿字节,太字节) 1PiB = 1024TB = 2^50 B;(Pebibyte,千万亿字节,拍字节) 1EiB = 1024PB = 2^60 B;(Exbibyte,百亿亿字节,艾字节) 1ZiB = 1024EB = 2^70 B;(Zebibyte,十万亿亿字节,泽字节) 1YiB = 1024ZB = 2^80 B;(Yobibyte,一亿亿亿字节,尧字节)
格式化字节大小
/**
* @title 格式化字节大小
* @author lyj [author] [2018-06-08]
* @param [type] $size [字节数]
* @param string $delimiter [数字和单位分隔符]
* @return [type] [格式化后的带单位的大小]
*/
function format_bytes($size, $delimiter = '')
{
$units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB', 'BB', 'NB', 'DB');
for ($i = 0; $size >= 1024 && $i < 11; $i++) {
$size /= 1024;
}
return round($size, 2) . $delimiter . $units[$i];
}
随机字符串
/**
* @title 随机字符串
* @author lyj [author] [2018-06-08]
* @param integer $length [字符长度]
* @param string $type [类型 数字 大写字母 小写字母 全部]
* @param integer $convert [大小写 大于0 大写 小于0 小写]
* @return [type] [description]
*
* echo random(32,'all',-45646);
*/
function random($length = 6, $type = 'all', $convert = 0)
{
$config = array(
'number' => '1234567890',
'capital_letter'=> 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'small_letter'=> 'abcdefghijklmnopqrstuvwxyz',
'all' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
);
if (!isset($config[$type])) {
$type = 'string';
}
$string = $config[$type];
$code = '';
$strlen = strlen($string) - 1;
for ($i = 0; $i < $length; $i++) {
$code .= $string{mt_rand(0, $strlen)};
}
if (!empty($convert)) {
$code = ($convert > 0) ? strtoupper($code) : strtolower($code);
}
return $code;
}
去除代码中的空白和注释
使用说明
$phpstr = file_get_contents("0608.php");
$phpstr_new = strip_whitespace($phpstr);
/**
* @title 去除代码中的空白和注释
* @author lyj [author] [2018-06-08]
* @param [type] $content [php文件内容]
* @return [type] [清空空白和注释后的代码]
*
*
* 使用说明
$phpstr = file_get_contents("0608.php");
$phpstr_new = strip_whitespace($phpstr);
*/
function strip_whitespace($content)
{
$stripStr = '';
//分析php源码
$tokens = token_get_all($content);
$last_space = false;
for ($i = 0, $j = count($tokens); $i < $j; $i++) {
if (is_string($tokens[$i])) {
$last_space = false;
$stripStr .= $tokens[$i];
} else {
switch ($tokens[$i][0]) {
//过滤各种PHP注释
case T_COMMENT:
case T_DOC_COMMENT:
break;
//过滤空格
case T_WHITESPACE:
if (!$last_space) {
$stripStr .= ' ';
$last_space = true;
}
break;
case T_START_HEREDOC:
$stripStr .= "<<<THINK\n";
break;
case T_END_HEREDOC:
$stripStr .= "THINK;\n";
for($k = $i+1; $k < $j; $k++) {
if(is_string($tokens[$k]) && $tokens[$k] == ';') {
$i = $k;
break;
} else if($tokens[$k][0] == T_CLOSE_TAG) {
break;
}
}
break;
default:
$last_space = false;
$stripStr .= $tokens[$i][1];
}
}
}
return $stripStr;
}
扫描目录
使用说明
scanDirs('.')
/**
* @title 扫描目录(过滤指定目录/文件),返回目录/文件 数组
* @author lyj [author] [2018-06-08]
* @param [type] $dir [description]
* @return [type] [description]
*
*/
function scanDirs($dir)
{
// 扫描文件
$files = scandir($dir);
// 过滤指定目录(文件)
$filter[] = '.';
$filter[] = '..';
$filter[] = '.git';
$filter[] = 'public';
$filter[] = '123.png';
// $filter = array();
// 过滤目录(获取交集 )
$files = array_diff($files, $filter);
return $files;
}
获取目录下的文件和目录
/**
* @title 获取目录下的文件和目录
* @author lyj [author] [2018-06-08]
* @param [type] $path [路径]
* @return [type] [description]
*
* 使用说明
* getFiles('.')
*/
function getFiles($path)
{
// 判断是否为目录 是则添加 /*
if (is_dir($path)) {
$path = $path . '/*';
}
$data = array();
$n = 0;
$m = 0;
$temp = glob($path);
// 遍历返回文件
foreach ($temp as $key => $file) {
if(is_file($file)){
$files[$n]['path'] = $file;
$files[$n]['name'] = basename($file);
$files[$n]['filesize'] = filesize($file);
$files[$n]['lasttime'] = date("Y-m-d H:i:s", filemtime($file));
$files[$n]['createtime'] = date("Y-m-d H:i:s", filectime($file));
$n++;
}
if(is_dir($file)){
$dirs[$m]['path'] = $file;
$dirs[$m]['name'] = basename($file);
$dirs[$m]['filesize'] = filesize($file);
$dirs[$m]['lasttime'] = date("Y-m-d H:i:s", filemtime($file));
$dirs[$m]['createtime'] = date("Y-m-d H:i:s", filectime($file));
$m++;
}
}
$data['files'] = $files;
$data['dirs'] = $dirs;
// $data[2] = $temp;
return $data;
}
获取当前访问的完整url地址
/**
* @title 获取当前访问的完整url地址
* @author lyj [author] [2018-06-12]
*/
function getCurUrl()
{
$url = isset ( $_SERVER ['HTTPS'] ) && $_SERVER ['HTTPS'] == 'on' ? 'https://' : 'http://';
if ($_SERVER ['SERVER_PORT'] != '80' && $_SERVER ['SERVER_PORT'] != '443') {
$url .= $_SERVER ['HTTP_HOST'] . ':' . $_SERVER ['SERVER_PORT'] . $_SERVER ['REQUEST_URI'];
} else {
$url .= $_SERVER ['HTTP_HOST'] . $_SERVER ['REQUEST_URI'];
}
// 兼容后面的参数组装
if (stripos ( $url, '?' ) === false) {
$url .= '?t=' . time ();
}
return $url;
}
字符串转成数组
/**
* @title 字符串转成数组,支持汉字,只能是utf-8格式的
* @author lyj [author] [2018-06-12]
* @param [type] $str [description]
*/
function stringToArray($str)
{
$result = array ();
$len = strlen ( $str );
$i = 0;
while ( $i < $len ) {
$chr = ord ( $str [$i] );
if ($chr == 9 || $chr == 10 || (32 <= $chr && $chr <= 126)) {
$result [] = substr ( $str, $i, 1 );
$i += 1;
} elseif (192 <= $chr && $chr <= 223) {
$result [] = substr ( $str, $i, 2 );
$i += 2;
} elseif (224 <= $chr && $chr <= 239) {
$result [] = substr ( $str, $i, 3 );
$i += 3;
} elseif (240 <= $chr && $chr <= 247) {
$result [] = substr ( $str, $i, 4 );
$i += 4;
} elseif (248 <= $chr && $chr <= 251) {
$result [] = substr ( $str, $i, 5 );
$i += 5;
} elseif (252 <= $chr && $chr <= 253) {
$result [] = substr ( $str, $i, 6 );
$i += 6;
}
}
return $result;
}
复制目录
/**
* @title 复制目录
* @author lyj [author] [2018-06-12]
* @param [type] $strSrcDir [description]
* @param [type] $strDstDir [description]
* @return [type] [description]
*/
function copyDir($strSrcDir, $strDstDir)
{
$dir = opendir ( $strSrcDir );
if (! $dir) {
return false;
}
if (! is_dir ( $strDstDir )) {
if (! mkdir ( $strDstDir )) {
return false;
}
}
while ( false !== ($file = readdir ( $dir )) ) {
if ($file == '.' || $file == '..' || $file == '.svn' || $file == '.DS_Store' || $file == '__MACOSX' || $file == 'Thumbs.db' || $file == 'Thumbs.db' || $file == 'info.php') {
continue;
}
if (is_dir ( $strSrcDir . '/' . $file )) {
if (! copydir ( $strSrcDir . '/' . $file, $strDstDir . '/' . $file )) {
return false;
}
} else {
if (! copy ( $strSrcDir . '/' . $file, $strDstDir . '/' . $file )) {
return false;
}
}
}
closedir ( $dir );
return true;
}
清空文件夹
/**
* @title 清空文件夹
* @author lyj [author] [2018-06-25]
* @param [type] $path [description]
* @return [type] [description]
*/
function delDir($path)
{
//如果是目录则继续
if(is_dir($path)){
//扫描一个文件夹内的所有文件夹和文件并返回数组
$p = scandir($path);
foreach($p as $val){
//排除目录中的.和..
if($val !="." && $val !=".."){
//如果是目录则递归子目录,继续操作
if(is_dir($path.$val)){
//子目录中操作删除文件夹和文件
deldir($path.$val.'/');
//目录清空后删除空文件夹
@rmdir($path.$val.'/');
}else{
//如果是文件直接删除
unlink($path.$val);
}
}
}
}
}
获取当前位置
/**
* 获取当前位置 thinkphp5
* @return 模块/控制器/方法
*/
function location()
{
$module = request()->module();
$controller = request()->controller();
$action = request()->action();
$localtion = $module.'/'.$controller.'/'.$action;
return $localtion;
}