>  기사  >  백엔드 개발  >  PHP惯用库函数

PHP惯用库函数

WBOY
WBOY원래의
2016-06-13 12:27:26779검색

PHP常用库函数

1.时间和日期

  • 如何获取时间戳 time()--从1970年开始计算的毫秒数

echo time();
  • 日期

echo date(<span style="color: #800000;">'</span><span style="color: #800000;">Y-m-d H:i:s</span><span style="color: #800000;">'</span>);
  • 获取默认是时区

echo date_default_timezone_get();
  • 默认获得的时间和本地电脑时间不一致,需要设置相应的时区

date_default_timezone_set('Asia/Shanghai'); //设置为上海的时区echo date(<span style="color: #800000;">'</span><span style="color: #800000;">Y-m-d H:i:s</span><span style="color: #800000;">'</span>);
  • 把时间戳转换成日期呈现出来

echo date(<span style="color: #800000;">'</span><span style="color: #800000;">Y-m-d H:i:s</span><span style="color: #800000;">'</span><span style="color: #000000;">,time());echo </span><span style="color: #800000;">'</span><span style="color: #800000;"><br></span><span style="color: #800000;">'</span><span style="color: #000000;">;echo date(</span><span style="color: #800000;">'</span><span style="color: #800000;">Y-m-d H:i:s</span><span style="color: #800000;">'</span>,time());

 

2.JSON格式数据的操作

  • JSON格式的数据

     数组可以嵌套(数组中包含数组)

     还可以包含对象(内部数据的值和名字相对应,键值对)

  [1,2,5,7,8,"Hello",[6,7,8],{"h","Hello"}]

  {"h":"Hello","w":"World",[1,2,3]}

  • 数组生成JSON格式的数据 encode

<span style="color: #800080;">$arr</span> = <span style="color: #0000ff;">array</span>(1,2,5,8,"Hello","CQUT",<span style="color: #0000ff;">array</span>("h"=>"Hello","name"=>"CQUT"<span style="color: #000000;">));</span><span style="color: #0000ff;">echo</span> 'array format => '.'<br>'<span style="color: #000000;">;</span><span style="color: #008080;">print_r</span>(<span style="color: #800080;">$arr</span><span style="color: #000000;">);</span><span style="color: #0000ff;">echo</span> '<br>'<span style="color: #000000;">;</span><span style="color: #0000ff;">echo</span> 'json formate =>'.'<br>'<span style="color: #000000;">;</span><span style="color: #0000ff;">echo</span> json_encode(<span style="color: #800080;">$arr</span>);<span style="color: #008000;">//</span><span style="color: #008000;">json_encode将一个对象转成json格式的数据</span>

  输出

  array format => 
  Array ( [0] => 1 [1] => 2 [2] => 5 [3] => 8 [4] => Hello [5] => CQUT [6] => Array ( [h] => Hello [name] => CQUT ) ) 
  json formate =>

  [1,2,5,8,"Hello","CQUT",{"h":"Hello","name":"CQUT"}]

  • 对象生成JSON格式的数据 encode

<span style="color: #800080;">$obj</span> = <span style="color: #0000ff;">array</span>('h'=>'Hello','w'=>'World',<span style="color: #0000ff;">array</span>(1,2,3<span style="color: #000000;">));</span><span style="color: #0000ff;">echo</span> json_encode(<span style="color: #800080;">$obj</span>);

  输出

  {"h":"Hello","w":"World","0":[1,2,3]}

  • 将JSON格式的数据转换成PHP对象 decode

<span style="color: #800080;">$jsonStr</span> = '{"h":"Hello","w":"World","0":[1,2,3]}'<span style="color: #000000;">;</span><span style="color: #800080;">$obj</span> = json_decode(<span style="color: #800080;">$jsonStr</span><span style="color: #000000;">);</span><span style="color: #008080;">print_r</span>(<span style="color: #800080;">$obj</span><span style="color: #000000;">);</span><span style="color: #0000ff;">echo</span> '<br>'<span style="color: #000000;">;</span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$obj</span>->h;

  输出

  stdClass Object ( [h] => Hello [w] => World [0] => Array ( [0] => 1 [1] => 2 [2] => 3 ) ) 

  Hello

 

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.