欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

自己写的另一个 time.helper.php

shiping1 的头像
<?php  if(!defined('DEDEINC')) exit('dedecms');
/**
 * 时间戳小助手
 *
 * @version        $Id: time.helper.php 1 2010-07-05 11:43:09Z tianya $
 * @package        DedeCMS.Helpers
 * @copyright      Copyright (c) 2007 - 2010, DesDev, Inc.
 * @link           http://www.dedecms.com
 */
 
/**
 *  返回格林威治标准时间
 *
 * @param     string  $format  字符串格式
 * @param     string  $timest  时间基准
 * @return    string
 */
if ( ! function_exists('MyDate'))
{
    function MyDate($format='Y-m-d H:i:s', $timest=0)
    {
        global $cfg_cli_time;
        $addtime = $cfg_cli_time * 3600;
        if(empty($format))
        {
            $format = 'Y-m-d H:i:s';
        }
        return gmdate ($format, $timest+$addtime);
    }
}
  
/**
 * 从普通时间转换为Linux时间截
 *
 * @param     string   $dtime  普通时间
 * @return    string
 */
if ( ! function_exists('GetMkTime'))
{
    function GetMkTime($dtime)
    {
        if(!preg_match("/[^0-9]/", $dtime))
        {
            return $dtime;
        }
        $dtime = trim($dtime);
        $dt = Array(1970, 1, 1, 0, 0, 0);
        $dtime = preg_replace("/[\r\n\t]|日|秒/", " ", $dtime);
        $dtime = str_replace("年", "-", $dtime);
        $dtime = str_replace("月", "-", $dtime);
        $dtime = str_replace("时", ":", $dtime);
        $dtime = str_replace("分", ":", $dtime);
        $dtime = trim(preg_replace("/[ ]{1,}/", " ", $dtime));
        $ds = explode(" ", $dtime);
        $ymd = explode("-", $ds[0]);
        if(!isset($ymd[1]))
        {
            $ymd = explode(".", $ds[0]);
        }
        if(isset($ymd[0]))
        {
            $dt[0] = $ymd[0];
        }
        if(isset($ymd[1])) $dt[1] = $ymd[1];
        if(isset($ymd[2])) $dt[2] = $ymd[2];
        if(strlen($dt[0])==2) $dt[0] = '20'.$dt[0];
        if(isset($ds[1]))
        {
            $hms = explode(":", $ds[1]);
            if(isset($hms[0])) $dt[3] = $hms[0];
            if(isset($hms[1])) $dt[4] = $hms[1];
            if(isset($hms[2])) $dt[5] = $hms[2];
        }
        foreach($dt as $k=>$v)
        {
            $v = preg_replace("/^0{1,}/", '', trim($v));
            if($v=='')
            {
                $dt[$k] = 0;
            }
        }
        $mt = mktime($dt[3], $dt[4], $dt[5], $dt[1], $dt[2], $dt[0]);
        if(!empty($mt))
        {
              return $mt;
        }
        else
        {
              return time();
        }
    }
}
  
/**
 *  减去时间
 *
 * @param     int  $ntime  当前时间
 * @param     int  $ctime  减少的时间
 * @return    int
 */
if ( ! function_exists('SubDay'))
{
    function SubDay($ntime, $ctime)
    {
        $dayst = 3600 * 24;
        $cday = ceil(($ntime-$ctime)/$dayst);
        return $cday;
    }
}
  
/**
 *  增加天数
 *
 * @param     int  $ntime  当前时间
 * @param     int  $aday   增加天数
 * @return    int
 */
if ( ! function_exists('AddDay'))
{
    function AddDay($ntime, $aday)
    {
        $dayst = 3600 * 24;
        $oktime = $ntime + ($aday * $dayst);
        return $oktime;
    }
}
  
/**
 *  返回格式化(Y-m-d H:i:s)的是时间
 *
 * @param     int    $mktime  时间戳
 * @return    string
 */
if ( ! function_exists('GetDateTimeMk'))
{
    function GetDateTimeMk($mktime)
    {
        return MyDate('Y-m-d H:i:s',$mktime);
    }
}
 
/**
 *  返回格式化(Y-m-d)的日期
 *
 * @param     int    $mktime  时间戳
 * @return    string
 */
if ( ! function_exists('GetDateMk'))
{
    function GetDateMk($mktime)
    {
        if($mktime=="0") return "暂无";
        else return MyDate("Y-m-d", $mktime);
    }
}
  
/**
 *  将时间转换为距离现在的精确时间
 *
 * @param     int   $seconds  秒数
 * @return    string
 */
if ( ! function_exists('FloorTime'))
{
    function FloorTime($seconds)
    {
        $times = '';
        $days = floor(($seconds/86400)%30);
        $hours = floor(($seconds/3600)%24);
        $minutes = floor(($seconds/60)%60);
        $seconds = floor($seconds%60);
        if($seconds >= 1) $times .= $seconds.'秒';
        if($minutes >= 1) $times = $minutes.'分钟 '.$times;
        if($hours >= 1) $times = $hours.'小时 '.$times;
        if($days >= 1)  $times = $days.'天';
        if($days > 30) return false;
        $times .= '前';
        return str_replace(" ", '', $times);
    }
}
  
//其实是去除最后一个
function removeLast($str){
$arr = explode(' > ', $str);
$count = count($arr);
$result = "";
for($i=0;$i<$count-1;$i++){
$i==0 && $result .=getSelfIndex($arr[$i]);
$i!=0 && $i!=$count-2 && $result .=' > '.$arr[$i];
$i!=0 && $i==$count-2 && $result .=' > ';
}
 
return $result;
}
//其实是去除最后一个
function removeLast_func($str){
return substr($str,0,strlen($str)-2);
}
 
//得到自定义的index
function getSelfIndex($indexStr){
$indexStr = preg_replace("#<a href='(.*)'>(.*)</a>#iUs","<a href='\${1}index.html'>\${2}</a>",$indexStr);
return $indexStr;
}
 
//看是单数 还是双数
function getEvenOdd($num)
{
return strval(intval($num%2));
}
//增加箭头
function addJiantou($str)
{
$arr   = explode(' > ', $str);
$count = count($arr);
// //假如最后一个不为空
$lastStr = trim($arr[$count-1]);
if(!empty($lastStr)){
return $str.' > ';
}
return $str;
  
}
 
function self_cn_substr($str,$len,$end='...')
{
if(strlen($str)<=$len){
return $str;
}
 
return cn_substr($str,$len).$end;
}
 
function getPpram($arcurl)
{
global $dsql;
$aidLength = strrpos($arcurl,'.')-strrpos($arcurl,'/')-1;
$aid = substr($arcurl,(strrpos($arcurl,'/')+1),$aidLength);
include_once (DEDEINC.'/arc.archives.class.php');
$arc = new Archives($aid);
$row = $dsql->GetOne("select channel from `#@__archives` where id='$aid'");
$channel = $row['channel'];
//不是图片类型 且不是 就业去向类型
if($channel!=18 && $channel!=19){
return $arcurl;
}
     
$row = $dsql->getOne("select typeid from `#@__arctiny` where id='$aid'");
$typeId = $row['typeid'];
//arcrank>=0 表示未删除
$dsql->SetQuery("select id from `#@__arctiny` where typeid='$typeId' and arcrank>=0 order by id desc");
$dsql->Execute();
$idArr = array();
 while($row = $dsql->GetArray())
      {
        $idArr[] = $row['id'];
      }
      //求第一个id,并且据此求得它的arcurl
      $firstId  = $idArr[0];
 
      $row = $dsql->getOne("select arc.*,tp.namerule,tp.typedir,tp.moresite,tp.siteurl,tp.sitepath from `#@__archives` arc left join `#@__arctype` tp   
       ON arc.typeid=tp.id where arc.id='$firstId'");
      $firstArcurl = GetFileUrl($row['id'],$row['typeid'],$row['senddate'],$row['title'],$row['ismake'],
      $row['arcrank'],$row['namerule'],$row['typedir'],$row['money'],$row['filename'],$row['moresite'],$row['siteurl'],$row['sitepath']);
      //下面两行 第一行反转   第二行是看自己的是第几个
      $idFlipArr = array_flip($idArr);
      $idFlipArr[$aid] = $idFlipArr[$aid]+1;
      //返回其实是此目录下的第一个文章的url后加上#p=0序号
      return $firstArcurl.'#p=0'.$idFlipArr[$aid];
}
function execJs($str){
echo "<script>".$str."</script>";
 
}
function alert($str){
echo "<script>alert('".$str."')</script>";
 
}
 
//根据img得到src路径 
function getImgSrc($str){
preg_match("/src='([^\']*)'?.*/i",$str, $matches);
return $matches[1];
}
  
 //史平忠的裁切功能  (只是裁切) //$fullImg 是原图像路径,$thumbImg 是新图像路径
 //$fix 是为了适应伍佰灵的 没有作用  $width是新图宽度 $height 是新图高度
 //最后参数一个wap  表示是否是从手机网站过来的 
function thumbImgCut($fullImg, $width=114 , $height=76 ,$thumbImg,$defaultWidth='100',$defaultHeight='100', $fix=5, $copyheightbi='', $wap='')
{
if(!is_file($fullImg)){
//echo $fullImg ."不存在<br>\n";
return ;
}
 
//list($w_src, $h_src) = getimagesize($fullImg);
$imgInfo=getimagesize($fullImg);
$w_src = $imgInfo[0];
$h_src = $imgInfo[1];
$imgType = $imgInfo[2];
//first change small by rate
if($width=='auto'){
$width = $height * $w_src/$h_src;
//假如自动的宽度大于默认宽度,就取默认宽度
$width>$defaultWidth && $width = $defaultWidth;
}
if($height=='auto'){
$height = $width * $h_src/$w_src;
//假如自动的高度大于默认宽度,就取默认高度
$height>$defaultHeight && $height = $defaultHeight;
}
 
$w_descFir    = $width;
$h_descFir    = $height;
$rate_descFir = $width/$height;
 
$rate_old = $w_src/$h_src;
//new rate >= old rate
if ($rate_descFir >= $rate_old)
{
 
//切掉东西的方法
$h_descFir = ceil($w_descFir / $rate_old);
}
//new rate < old rate
else if ($rate_descFir < $rate_old)
{
//切掉东西的方法
$w_descFir = ceil($h_descFir * $rate_old);
 
}
//source photo
//gif
if ($imgType==1)
{
$srcPho  = imagecreatefromgif($fullImg);
}//jpeg(jpg)
else if ($imgType==3)
{
$srcPho  = imagecreatefrompng($fullImg);
}//其余假定为 jpeg 其实是$imgType==2
else
{
$srcPho  = imagecreatefromjpeg($fullImg);
}
//first new small photo
$descPhoFir = imagecreatetruecolor($w_descFir, $h_descFir);
imagecopyresampled($descPhoFir, $srcPho, 0, 0, 0, 0, $w_descFir, $h_descFir, $w_src, $h_src);
//second change small by crop
if ($rate_descFir >= $rate_old)
{
$cropX  = 0;
if(!empty($copyheightbi)){
$copyheightbi = 1/$copyheightbi;
$cropY =  ceil(($h_descFir-$height)/$copyheightbi);
}else{   //假如没有 的话 就从减去的1/2 ;;;换句话说 就是只取中间,上下裁去相等
$cropY =  ceil(($h_descFir-$height)/2);
}
 
$h_descFir = $height;
}
else if ($rate_descFir < $rate_old)
{
$cropX  = ceil(($w_descFir-$width)/2);
$cropY =  0;
$w_descFir = $width;
}
//second new small photo
$descPhoSec = imagecreatetruecolor($width, $height);
imagecopyresampled($descPhoSec, $descPhoFir, 0, 0, $cropX, $cropY, $width, $height, $width, $height);
//生成gif
if ($imgType==1)
{
imagegif($descPhoSec, $thumbImg);
}//生成png
else if ($imgType==3)
{
Imagepng($descPhoSec, $thumbImg);
} //其余生成jpg(jpeg)
else 
{
$wap=='wap' ? $jingdu=90 : $jingdu=90;
ImageJpeg($descPhoSec, $thumbImg, $jingdu);
}
@imagedestroy($photo);
 
chmod($thumbImg, 0777);
}
 
 function endsWith($haystack, $needle)
 {   
  $length = strlen($needle);  
  if($length == 0) 
  {    
  return true;  
  }  
  return (substr($haystack, -$length) === $needle);
 }
 
function makeMulitImg($path,$get){
 
$pointPosition = strrpos($path, '.');
$prevPath = substr($path,0,$pointPosition);
$suffPath = strrchr($path, '.');
if(file_exists($prevPath.'_auto'.$suffPath)){
return ;
}
//感恩母校栏目页 主图
$additi ='';
$get['CKEditor']=='body' && $additi='-lp';
$thumbImg =  $prevPath.$additi.'_auto_2'.$suffPath;
ImageResizeNew($path, '176', '250',  $thumbImg, $issave=TRUE);
$thumbImg =  $prevPath.$additi.'_auto_4'.$suffPath;
ImageResizeNew($path, '150', '215',  $thumbImg, $issave=TRUE);
  
}
   
//得到img指定的(大,或中或小)路径
//值有 auto 100 160 170 180 180_2 200 215 400
//这些值指的是自定义裁切的图的宽度
 //最后参数一个wap  表示是否是从手机网站过来的 
 //最后参数一个wap  表示是否是从手机网站过来的 
 function getReferImg($litpic, $width='100', $height='100', $defautWidth='100', $defaultHeight='100',$copyheightbi='',$wap='')
 {
 
  //error_reporting(0);
  include_once(DEDEINC.'/image.func.php');
  include_once('image.helper.php');
  //我们是先生成,再取出图片
$pointPosition = strrpos($litpic, '.');
$prevPath = substr($litpic,0,$pointPosition);
$suffPath = strrchr($litpic, '.'); 
 
//没有  $copyheightbi 即没有裁切点的时候
if(empty($copyheightbi)){
$newLitpic = $prevPath.'-'.$width.'-'.$height.$suffPath;
}else{  //有裁切点的时候
$newLitpic = $prevPath.'-'.$width.'-'.$height.'-'.$copyheightbi.$suffPath;
}
  
$newPicPath = $_SERVER['DOCUMENT_ROOT'].$newLitpic;
 
//假如是手机网站的时候
if($wap=='wap'){
$newPicPath = $_SERVER['DOCUMENT_ROOT'].'/wap'.$newLitpic;
if(!is_dir(dirname($newPicPath))){
mkdirAll(dirname($newPicPath));
}
}
 
//看是否存在这个裁剪的新图,有的话 就直接返回
if(file_exists($newPicPath)){
return $newLitpic;
}
$oldPicPath = $_SERVER['DOCUMENT_ROOT'].$litpic;
thumbImgCut($oldPicPath,$width,$height,$newPicPath,$defautWidth,$defaultHeight,'5',$copyheightbi,$wap);
return $newLitpic;
 }
    
//根据宽高比与原图的宽高比 来调用 thumbImgCut
function thumbImgCutByRate($oldPicPath,$width,$height,$newPicPath)
{
$imgInfo=getimagesize($oldPicPath);
//原图宽高
$w_src = $imgInfo[0];
$h_src = $imgInfo[1];
$rateOld =  $w_src/$h_src;
$rateNew =  $width/$height;
//看比率 ,得到 所要的宽度 或高度
if($rateNew>$rateOld){
$width = floor($height * $rateOld);
}
else {
$height = floor($width /$rateOld);
}
thumbImgCut($oldPicPath, $width, $height,$newPicPath);
}
//根据宽高比得到指定的图形对象
function getReferImgByRate($litpic, $width='100', $height='100')
{
  //error_reporting(0);
  include_once(DEDEINC.'/image.func.php');
  include_once('image.helper.php');
  //我们是先生成,再取出图片
$pointPosition = strrpos($litpic, '.');
$prevPath = substr($litpic,0,$pointPosition);
$suffPath = strrchr($litpic, '.'); 
$newLitpic = $prevPath.'-'.'rate'.'-'.$width.'-'.$height.$suffPath;
$newPicPath = $_SERVER['DOCUMENT_ROOT'].$newLitpic;
//看是否存在这个裁剪的新图,有的话 就直接返回
if(file_exists($newPicPath)){
return $newLitpic;
}
$oldPicPath = $_SERVER['DOCUMENT_ROOT'].$litpic;
thumbImgCutByRate($oldPicPath,$width,$height,$newPicPath);
return $newLitpic;
 }
 
 //看是否宽度超过540,超过的话 就裁
 function getReferImgWidthMax($litpic,$width='540',$height='auto')
 {
  //error_reporting(0);
  include_once(DEDEINC.'/image.func.php');
  include_once('image.helper.php');
  //我们是先生成,再取出图片
$pointPosition = strrpos($litpic, '.');
$prevPath = substr($litpic,0,$pointPosition);
$suffPath = strrchr($litpic, '.'); 
$newLitpic = $prevPath.'-'.'widthMax'.'-'.$width.'-'.$height.$suffPath;
$newPicPath = $_SERVER['DOCUMENT_ROOT'].$newLitpic;
//看是否存在这个裁剪的新图,有的话 就直接返回
if(file_exists($newPicPath)){
return $newLitpic;
}
$oldPicPath = $_SERVER['DOCUMENT_ROOT'].$litpic;
$imgInfo=getimagesize($oldPicPath);
//原图宽
$w_src = $imgInfo[0];
//假如原图的宽度小于要设定的宽度540,返回原图
if($w_src<$width){
return $litpic;
}
 
$oldPicPath = $_SERVER['DOCUMENT_ROOT'].$litpic;
//给个默认的宽度和高度 ;当它的高度  高于默认的高度时 取默认高度
thumbImgCut($oldPicPath,$width,$height,$newPicPath,'5000','5000');
return $newLitpic;
 }
  
  //看是否高度超过540,超过的话 就裁
 function getReferImgHeightMax($litpic,$width='auto',$height='540')
 {
  //error_reporting(0);
  include_once(DEDEINC.'/image.func.php');
  include_once('image.helper.php');
  //我们是先生成,再取出图片
$pointPosition = strrpos($litpic, '.');
$prevPath = substr($litpic,0,$pointPosition);
$suffPath = strrchr($litpic, '.'); 
$newLitpic = $prevPath.'-'.'heightMax'.'-'.$width.'-'.$height.$suffPath;
$newPicPath = $_SERVER['DOCUMENT_ROOT'].$newLitpic;
//看是否存在这个裁剪的新图,有的话 就直接返回
if(file_exists($newPicPath)){
return $newLitpic;
}
$oldPicPath = $_SERVER['DOCUMENT_ROOT'].$litpic;
$imgInfo=getimagesize($oldPicPath);
 
//原图高
$h_src = $imgInfo[1];
//假如原图的高度小于要设定的高度540,返回原图
if($h_src<$height){
return $litpic;
}
 
$oldPicPath = $_SERVER['DOCUMENT_ROOT'].$litpic;
//给个默认的宽度和高度 ;当它的宽度  高于默认的宽度时 取默认高度
thumbImgCut($oldPicPath,$width,$height,$newPicPath,'5000','5000');
return $newLitpic;
 }
 
 //去掉首尾的中英文空格
 function removeCnEnSpace($str)
 {
  return mb_ereg_replace('(^( | )+|( | )+$)', '', $str); 
   
 }
 //去掉首尾&nbsp;&nbsp;&nbsp;&nbsp;
 function removeNbsp($str)
 {
 
  $str =  preg_replace('/^(&nbsp;)*/' , '', $str);
 
  return  preg_replace('/(&nbsp;)*$/' , '', $str);
 }
 
 //称除回车tab键
 function removeEnterTab($str)
 {
 
  return preg_replace('/[\n\r\t]/', '',$str);
 }
  
 //先去标签 ,再进行截取
 function self_cn_substr_strip_tag($str,$len,$end='...')
 {
  //移除回车 tab键
  $str = removeEnterTab($str);
  //先去掉首尾的中英文空格
  $str = removeCnEnSpace($str);
  //去掉标签
  $str = trim(dede_strip_tags($str));
     
  //再去掉首尾&nbsp;&nbsp;&nbsp;&nbsp;
  $str = removeNbsp($str);
   
  return self_cn_substr($str, $len, $end);
 
 }
  
 //解析模板标签 
function pasterTempletDiy($path)
{
require_once(DEDEINC."/arc.partview.class.php");
global $cfg_basedir,$cfg_templets_dir;
$tmpfile = $cfg_basedir.$cfg_templets_dir."/".$path;//模版文件的路径
$dtp = new PartView();
$dtp->SetTemplet($tmpfile);
$dtp->Display();
}
 
//返回顶部tid ;
//function getTopId($tid)
//{
// error_reporting(E_ALL);
// global $dsql;
// if(!empty($tid)){
// return 0;
// }
//
// //得到topid
// $sql =$dsql->GetOne("SELECT topid FROM `#@__arctype` WHERE id='$tid'");
// $topId = $row['topid'];
// //如果topId为0的话 其实就是自己就是topId
// $topId==0 && $topId=$tid;
// return $topId;
//
//}
 
//根据aid得到tid
function getTidByAid($aid)
{
 
global $dsql;
$tid = 0;
if(!empty($aid)){
$row = $dsql->getOne("select typeid from `#@__archives` where id='$aid'");
$tid = $row['typeid'];
}
return $tid;
 
}
    
//根据tid来决定采用hover的样式
function getHover($oldTid)
global $tid,$aid;
if(empty($tid) && !empty($aid)){
$tid = getTidByAid($aid);
}
 
$topId=GetTopid($tid);
  
$allIdsIncludeSon = GetSonIds($topId,0);
 
if($topId == $oldTid || $tid==$oldTid){
return "class='hover'";
}
else {
return '';
}
 
}
 
//自定义路径
function self_position($str){
preg_match_all("/<a(.*?)<\/a>/ism",$str,$a,PREG_PATTERN_ORDER);
$aArr = $a[0];
$result = "";
foreach($aArr as $key=>$value)
{
$result .=$value."<span class='subpage_menu_bg'></span>";
}
return $result;
 
}
 
//自定义路径
function self_position_second($str){
preg_match_all("/<a(.*?)<\/a>/ism",$str,$a,PREG_PATTERN_ORDER);
$aArr = $a[0];
$result = "";
foreach($aArr as $key=>$value)
{
$result .=$value."<span class='subpage_menu_bg'></span>";
}
return $result;
 
}
 
//重写上一个链接
function preimglink($str)
{
preg_match_all("/href='(.*?)'/ism",$str,$a,PREG_PATTERN_ORDER);
$hrefArr = $a[1];
if(!empty($hrefArr)){
return "<a href='".$hrefArr[0]."' class='previmg' title='上一个'> </a>";
}
else{
return '';
}
 
}
//重写下一个链接
function nextimglink($str)
{
preg_match_all("/href='(.*?)'/ism",$str,$a,PREG_PATTERN_ORDER);
$hrefArr = $a[1];
if(!empty($hrefArr)){
return "<a href='".$hrefArr[0]."' class='nextimg' title='下一个'> </a>";
}
else{
return '';
}
 
}
  
//数字转成汉字
function  getHanziNum($num)
{
$charArr = array('零','一','二','三','四','五','六','七','八','九');
return $charArr[$num];
}
 
//数字转成圆角数字
function getYuanziNum($num)
$charArr = array('0','⑴','⑵','⑶','⑷','⑸','⑹','⑺','⑻','⑼','⑽','⑾','⑿','⒀','⒁','⒂');
return $charArr[$num];
}
  
//得到*串
 
function getXingHao($str,$start=1,$length=1){
 
// error_reporting(E_ALL);//
$repeatedStr = str_repeat('*',$length);//
$str = substr_replace($str,$repeatedStr,$start,$length);
return $str;
  
//以下其实是进行正则替换
function addMaodian($str)
{
 
$returnStr = preg_replace('/href=["\'](.*)["\']/iUs',"href='$1#toTu'",$str);
return $returnStr;
}
  
function MyDateHanziyue($timestamp)
{
$day   = MyDate('d', $timestamp);
$month = MyDate('n', $timestamp);
$year  = MyDate('Y', $timestamp);
$arr = array('零 月','一 月','二 月','三 月','四 月','五 月','六 月','七 月','八 月','九 月','十 月','十 一 月','十 二 月');
return ''.$day.' '.$arr[$month].' '.$year;
 
}
普通分类: