Heim > Artikel > Backend-Entwicklung > PHP惯用库函数
PHP常用库函数
1.时间和日期
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格式数据的操作
数组可以嵌套(数组中包含数组)
还可以包含对象(内部数据的值和名字相对应,键值对)
[1,2,5,7,8,"Hello",[6,7,8],{"h","Hello"}]
{"h":"Hello","w":"World",[1,2,3]}
<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"}]
<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]}
<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