PHP常用库函数
1.时间和日期
如何获取时间戳 time()--从1970年开始计算的毫秒数echo time();
日期echo date('Y-m-d H:i:s');
获取默认是时区echo date_default_timezone_get();
默认获得的时间和本地电脑时间不一致,需要设置相应的时区date_default_timezone_set('Asia/Shanghai');//设置为上海的时区echo date('Y-m-d H:i:s');
把时间戳转换成日期呈现出来echo date('Y-m-d H:i:s',time());
echo'<br/>';
echo date('Y-m-d H:i:s',time());
2.JSON格式数据的操作
JSON格式的数据 数组可以嵌套(数组中包含数组)
还可以包含对象(内部数据的值和名字相对应,键值对)
[1,2,5,7,8,"Hello",[6,7,8],{"h","Hello"}]
{"h":"Hello","w":"World",[1,2,3]}
数组生成JSON格式的数据 encode$arr=array(1,2,5,8,"Hello","CQUT",array("h"=>"Hello","name"=>"CQUT"));echo'array format => '.'<br/>';PRint_r($arr);echo'<br/>';echo'json formate =>'.'<br/>';echojson_encode($arr);//json_encode将一个对象转成json格式的数据
输出
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$obj=array('h'=>'Hello','w'=>'World',array(1,2,3));echojson_encode($obj);
输出
{"h":"Hello","w":"World","0":[1,2,3]}
将JSON格式的数据转换成php对象 decode$jsonStr= '{"h":"Hello","w":"World","0":[1,2,3]}';$obj= json_decode($jsonStr);print_r($obj);echo'<br/>';echo$obj->h;
输出
stdClass Object ( [h] => Hello [w] => World [0] => Array ( [0] => 1 [1] => 2 [2] => 3 ) )
Hello