php日期处理函数(计算时间差,转换时间戳日期)

 更新时间:2016年11月25日 17:40  点击:2073

php教程 转换时间戳为常用的日期格式与计算时间差:默认返回类型为“分钟”
function trans_time($timestamp){
 if($timestamp < 1) echo '无效的unix时间戳';
 else return date("y-m-d h:i:s",$timestamp);
}

//获取ip
function get_ip() {
    if ($_server["http_x_forwarded_for"])
        $ip = $_server["http_x_forwarded_for"];
    else if ($_server["http_client_ip"])
        $ip = $_server["http_client_ip"];
    else if ($_server["remote_addr"])
        $ip = $_server["remote_addr"];
    else if (getenv("http_x_forwarded_for"))
        $ip = getenv("http_x_forwarded_for");
    else if (getenv("http_client_ip"))
        $ip = getenv("http_client_ip");
    else if (getenv("remote_addr"))
        $ip = getenv("remote_addr");
    else
        $ip = "unknown";
    return $ip;
}

//计算时间差:默认返回类型为“分钟”
//$old_time 只能是时间戳,$return_type 为 h 是小时,为 s 是秒
function timelag($old_time,$return_type='m'){
 if($old_time < 1){
  echo '无效的unix时间戳';
 }else{
  switch($return_type){
   case 'h':
   $type = 3600; break;
   case 'm':
   $type = 60; break;
   case 's':
   $type = 1; break;
   case '':
   $type = 60; break;
  }
  $dif = round( (time()-$old_time)/$type ) ;
  return $dif;
 }
}

数原型:string iconv ( string $in_charset , string $out_charset , string $str )
特别是第二个参数说明:
the output charset.

用iconv()转换一个输出字符编码不支持的字符时,如iconv('utf-8', 'gb2312', 'www.111cn.net'),会遇到这样的错误提示:

notice: iconv() [function.iconv]: detected an illegal character in input string ...

因为gb2312表示的是简体中文,不支持像"www.111cn.net"之类的更为复杂的汉字以及一些特殊字符,这当然会报错了,解决办法有两种:

1. 扩大输出字符编码的范围,如iconv('utf-8', 'gbk', 'www.111cn.net'),则可以正确地输出,因为gbk支持的字符范围更广;

2. 在输出的字符编码字符串后面加上"//ignore",如iconv('utf-8', 'gb2312//ignore', 'www.111cn.net'),这样做其实是忽略了不能转换的字符,避免了出错但却不能够正确地输出(即空白不、输出)。


下面来看看关于php教程 iconv() : detected an illegal character in input string处理方法

$str = iconv('utf-8', 'gbk//ignore', unescape(isset($_get['str'])? $_get['str']:''));
本地测试//ignore能忽略掉它不认识的字接着往下转,并且不报错,而//translit是截掉它不认识的字及其后面的内容,并且报错。//ignore是我需要的。
现在等待上线看结果(这样不是好的做法,继续琢磨手册,上网搜搜看),呵呵。。。

在网上找到下面这篇文章,发现mb_convert_encoding也可以,但效率比iconv差。


转换字符串编码iconv与mb_convert_encoding的区别

iconv — convert string to requested character encoding(php 4 >= 4.0.5, php 5)
mb_convert_encoding — convert character encoding(php 4 >= 4.0.6, php 5)

用法:
string mb_convert_encoding ( string str, string to_encoding [, mixed from_encoding] )
需要先启用 mbstring 扩展库,在 php.ini里将; extension=php_mbstring.dll 前面的 ; 去掉

string iconv ( string in_charset, string out_charset, string str )
注意:
第二个参数,除了可以指定要转化到的编码以外,还可以增加两个后缀://translit 和 //ignore,
其中:
//translit 会自动将不能直接转化的字符变成一个或多个近似的字符,
//ignore 会忽略掉不能转化的字符,而默认效果是从第一个非法字符截断。
returns the converted string or false on failure.

使用:
1. 发现iconv在转换字符"-"到gb2312时会出错,如果没有ignore参数,所有该字符后面的字符串都无法被保存。不管怎么样,这个"-"都无法转换成功,无法输出。另外mb_convert_encoding没有这个bug.
2. mb_convert_encoding 可以指定多种输入编码,它会根据内容自动识别,但是执行效率比iconv差太多;如:$str = mb_convert_encoding($str,"euc-jp","ascii,jis,euc-jp,sjis,utf- 8");“ascii,jis,euc-jp,sjis,utf-8”的顺序不同效果也有差异
3. 一般情况下用 iconv,只有当遇到无法确定原编码是何种编码,或者iconv转化后无法正常显示时才用mb_convert_encoding 函数

from_encoding is specified by character code name before conversion. it can be array or string - comma separated enumerated list. if it is not specified, the internal encoding will be used.

$str = mb_convert_encoding($str, "ucs-2le", "jis, eucjp-win, sjis-win");
$str = mb_convert_encoding($str, "euc-jp', "auto");

网页更改地址后想要对搜索引擎友好的最好措施就是做301重定向,如果不是暂时把某个或某些网页转移的话,我们都认为做301重定向比较合适。

下面一款php教程常用的301重定向代码

<?
header( "http/1.1 301 moved permanently" ) ;
header( "location: http://www.111cn.net/" );
?>

301重定向是永久性重定向,当用户或者搜索引擎向我们的网站发出浏览请求时,网站服务器返会返还http 数据流,该数据流头信息(header)里包含某种状态码,301 就是表示本网页永久性转移到另一个地址的一个状态码

笔记,关于header函数

header(string,replace,http_response_code)

header() 函数向客户端发送原始的 http 报头。

认识到一点很重要,即必须在任何实际的输出被发送之前调用 header() 函数(在 php 4 以及更高的版本中,您可以使用输出缓存来解决此问题):

打开jpgraph.php教程文件,找到
private $font_family=ff_font1,$font_style=fs_normal,$font_size=12;

private $font_family=ff_simsun,$font_style=fs_normal,$font_size=8;

在php中编码转换函数常用的有mb_convert_encoding与iconv两个函数,两个函数的区别在于,后者执行效率比mb_convert_encoding快速很多,iconv在转换字符\"—\"到gb2312时会出错,如果没有ignore参数,

所有该字符后面的字符串都无法被保存。不管怎么样,这个"—"都无法转换成功,无法输出。另外mb_convert_encoding没有这个bug.
  */

header("content-type: text/html; charset=utf-8");
echo mb_convert_encoding("你是我的友仔", "utf-8", "gbk");

//gbk to gb2312

 

header("content-type: text/html; charset=big5");
echo mb_convert_encoding("你是我的朋友", "big5", "gb2312");

 

$content = iconv("gbk", "utf-8", $content);
$content = mb_convert_encoding($content, "utf-8", "gbk");

/*


一般情况下用 iconv,只有当遇到无法确定原编码是何种编码,或者iconv转化后无法正常显示时才用mb_convert_encoding 函数.

花絮:如果你不知道你当前字符编码可以使用mb_detect_encoding来检测你字符编码哦

相关文件请查看
http://www.111cn.net/phper/php-cy/36263.htm

[!--infotagslink--]

相关文章