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

这里的技术是共享的

You are here

一个ok的 time.helper.php

<?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 execJs($str){
 
    echo "<script>".$str."</script>";
 
 
 
}
 
function alert($str){
 
    echo "<script>alert('".$str."')</script>";
 
 
 
}
 
 
 
 
 
//解析模板标签
 
function pasterTempletDiy($filename)
{
 
    require_once(DEDEINC."/arc.partview.class.php");
 
 
 
    global $cfg_basedir,$cfg_templets_dir,$cfg_df_style;
 
    //服务器 本地不同
 
    //var_dump($cfg_basedir.$cfg_templets_dir.'/'.$cfg_df_style.'/'.$filename);
    if( file_exists($cfg_basedir.$cfg_templets_dir.'/'.$cfg_df_style.'/'.$filename)){
        $okfile = $cfg_basedir.$cfg_templets_dir.'/'.$cfg_df_style.'/'.$filename;
 
    }
 
    $dtp = new PartView();
 
 
    $dtp->SetTemplet($okfile);
 
    $dtp->Display();
 
}
 
 
 
//解析模板标签
 
function pasterTempletStr($str)
 
{
 
 
 
 
    require_once(DEDEINC."/arc.partview.class.php");
 
 
 
    $dtp = new PartView();
 
 
 
    $dtp->SetTemplet($str,'string');
 
    ob_start();
 
    $dtp->Display();
 
    $return = ob_get_contents();
 
    ob_end_clean();
 
 
    return $return;
 
}
 
//根据图片 数组 得到每一个图片 再组装成 html
 
function getEveryPic($arr)
 
{
 
    $return = '';
 
    $picarr = $arr['pic'];
 
    foreach($picarr as $key=>$value)
 
    {
 
        $return .= "<p class='oneimg'><img src='".getReferImg($value,145,200)."'  class='tobigtu tobigtuimg' tobigtu='".getReferImgHeightMax($value,640,'auto')."' /><a href='javascript:void(0);' class='fangda tobigtu'  tobigtu='".getReferImgHeightMax($value,640,'auto')."' alt='".$arr['content']."'><!--<img src='/templets/zhengxing/images/weixin/fangda.png' alt='' width='32' height='32' >--></a></p>";
 
    }
 
    return $return;
 
}
 
 
 
//根据数字分页 组装成 数字 html分页
 
function getNumPage($arr)
 
{
 
    $return = '';
 
    $num_pages = $arr['pages'];
 
 
 
    foreach($num_pages as $key_page=>$value_page)
 
    {
 
        if($arr['curr_page'] == $value_page ){
 
            $return .= "<a href='?p=".$value_page."' class='current' >".$value_page."</a>";
 
 
 
        }else{
 
            $return .= "<a href='?p=".$value_page."' >".$value_page."</a>";
 
        }
 
    }
 
    return $return;
 
}
 
 
 
//根据数字分页 组装成 select html分页
 
function getSelectPage($arr)
 
{
 
    $return = '';
 
    $select_pages = $arr['allpages'];
 
    $return .= "<select name='selectpage' id='selectpage'>";
 
    foreach($select_pages as $key_all_page=>$value_all_page)
 
    {
 
        if($arr['curr_page'] == $value_all_page ){
 
            $return .=  "<option value='".$value_all_page."' selected='selected' >第".$value_all_page."页</option>";
 
        }
 
        else {
 
            $return .=  "<option value='".$value_all_page."'>第".$value_all_page."页</option>";
 
        }
 
    }
 
    $return .= "</select>";
 
    return $return;
 
 
 
 
 
}
 
 
 
 
 
if ( ! function_exists('thumbImgCut'))
 
{
 
//史平忠的裁切功能  (只是裁切) //$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=100 : $jingdu=100;
 
            ImageJpeg($descPhoSec, $thumbImg, $jingdu);
 
        }
 
        @imagedestroy($descPhoSec);
 
 
 
        chmod($thumbImg, 0777);
 
    }
 
}
 
 
 
if ( ! function_exists('getReferImg'))
 
{
 
    function getReferImg($litpic, $width='100', $height='100', $defautWidth='5000', $defaultHeight='5000',$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;
 
    }
 
}
 
 
 
//看是否宽度超过540,超过的话 就裁
 
if ( ! function_exists('getReferImgWidthMax'))
 
{
 
    function getReferImgWidthMax($litpic,$width='auto',$height='auto',$defautWidth='5000', $defaultHeight='5000')
 
    {
 
        //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;
 
 
        $oldPicPath = $_SERVER['DOCUMENT_ROOT'].$litpic;
 
        $imgInfo=getimagesize($oldPicPath);
 
        //原图宽
 
        $w_src = $imgInfo[0];
 
        //假如原图的宽度小于要设定的宽度540,返回原图
 
        if($w_src<$width){
 
            return $litpic;
 
        }
 
        //看是否存在这个裁剪的新图,有的话 就直接返回
 
        if(file_exists($newPicPath)){
 
            return $newLitpic;
 
        }
 
 
        //给个默认的宽度和高度 ;当它的高度  高于默认的高度时 取默认高度
 
        thumbImgCut($oldPicPath,$width,$height,$newPicPath,$defautWidth,$defaultHeight);
 
        return $newLitpic;
 
    }
 
}
 
 
//看是否宽度超过540,超过的话 就裁
function getReferImgHeightMax($litpic,$width='auto',$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);
    //原图宽
    $h_src = $imgInfo[0];
    //假如原图的宽度小于要设定的宽度540,返回原图
    if($h_src<$height){
        return $litpic;
    }
 
    $oldPicPath = $_SERVER['DOCUMENT_ROOT'].$litpic;
    //给个默认的宽度和高度 ;当它的高度  高于默认的高度时 取默认高度
    thumbImgCut($oldPicPath,$width,$height,$newPicPath,'5000','5000');
    return $newLitpic;
}
//返回typeurl
function getBaomingUrl($tid)
 
{
 
    global $dsql;
 
    $query2 = "SELECT * FROM #@__arctype where id = $tid";
 
    $row = $dsql->GetOne($query2);
 
    $typeurl  = GetTypeUrl($row['id'],MfTypedir($row['typedir']),$row['isdefault'],$row['defaultname'],$row['ispart'],$row['namerule2'],$row['moresite'],$row['siteurl'],$row['sitepath']);
 
    return $typeurl;
 
}
 
function getPasterTempletDiy($filename)
 
{
 
    require_once(DEDEINC."/arc.partview.class.php");
 
 
 
    global $cfg_basedir,$cfg_templets_dir,$cfg_df_style;
 
    //服务器 本地不同
 
    //var_dump($cfg_basedir.$cfg_templets_dir.'/'.$cfg_df_style.'/'.$filename);
 
    if( file_exists($cfg_basedir.$cfg_templets_dir.'/'.$cfg_df_style.'/'.$filename)){
 
        $okfile = $cfg_basedir.$cfg_templets_dir.'/'.$cfg_df_style.'/'.$filename;
 
    }
 
    $dtp = new PartView();
 
 
 
    $dtp->SetTemplet($okfile);
 
    ob_start();
 
    $dtp->Display();
 
    $return = ob_get_contents();
 
    ob_end_clean();
 
    return $return;
 
 
 
 
 
}
 
function getCustomPre($str)
{
 
    $str = str_replace('上一篇:',"<span> &nbsp;上一篇 &nbsp;</span> &nbsp;",$str);
    return $str;
}
 
function getCustomNext($str)
{
    $str = str_replace('下一篇:',"<span> &nbsp;下一篇 &nbsp;</span> &nbsp;",$str);
    return $str;
}
 
//去掉首尾的中英文空格
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);
}
 
function self_cn_substr($str,$len,$end='...')
{
    if(strlen($str)<=$len){
        return $str;
    }
 
    return cn_substr($str,$len).$end;
}
 
//称除回车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 removeJiLu($str)
{
    return str_replace('记录','',$str);
}
 
//取得微信图的第一张照片 先进行保存 然后进行按宽度 高度缩小
function getFirstImgFromOtherSite($str,$width,$height)
{
    error_reporting(E_ALL);
    include_once(DEDEROOT.'/pub/functions.php');
    $pic = unserialize($str);
    saveFromWapPic($pic);
    return getReferImg($pic[0],$width,$height);
}
普通分类: