码迷,mamicode.com
首页 > Web开发 > 详细

项目开发中常用的PHP函数

时间:2014-07-22 23:54:18      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:style   color   使用   strong   io   art   

日期操作

为了便于存储、比较和传递,我们通常需要使用strtotime()函数将日期转换成UNIX时间戳,只有在显示给用户看的时候才使用date()函数将日期转换成常用的时间格式。

strtotime()  函数将任何英文文本的日期时间描述解析为 Unix 时间戳

eg:

<?php
echo(strtotime("now"));
echo(strtotime("3 October 2005"));
echo(strtotime("+5 hours"));
echo(strtotime("+1 week"));
echo(strtotime("+1 week 3 days 7 hours 5 seconds"));
echo(strtotime("next Monday"));
echo(strtotime("last Sunday"));
?>

输出:

1138614504
1128290400
1138632504
1139219304
1139503709
1139180400
1138489200


date()函数 将时间戳转换成常用的日期格式

eg:

echo date(‘Y-m-d H:i:s‘,"1138614504");

输出:

2006-01-30 17:48:24


字符串操作

有时候需要取得某个字符串的一部分,就需要用到字符串的截取substr()函数

substr()函数返回字符串的一部分

语法:

substr(string,start,length)

eg:

echo substr("Hello world!",6,5);

输出:

world


数组操作

这里介绍两个非常实用的函数:

array_unique()移除数组中相同元素的个数

当几个数组元素的值相等时,只保留第一个元素,其他的元素被删除。
返回的数组中键名不变。


array_filter()删除数组中为空的元素

语法:

array array_filter ( array $input [, callable $callback = "" ] )

依次将 input 数组中的每个值传递到 callback 函数。如果 callback 函数返回 TRUE,则 input 数组的当前值会被包含在返回的结果数组中。数组的键名保留不变。

input为要循环的数组
callback为使用的回调函数,如果没有提供 callback 函数,将删除 input 中所有等值为 FALSE 的条目(可以利用这条删除数组中为空的元素)。

eg1:

<?php
function odd($var){
    return($var & 1);
}
$array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);
echo "Odd :\n";
print_r(array_filter($array1, "odd"));
?>

输出:

Odd :
Array
(
    [a] => 1
    [c] => 3
    [e] => 5
)

eg2:

<?php
$entry = array(
             0 => 'foo',
             1 => false,
             2 => -1,
             3 => null,
             4 => ''
          );

print_r(array_filter($entry));
?>

输出:

Array
(
    [0] => foo
    [2] => -1
)


项目开发中常用的PHP函数,布布扣,bubuko.com

项目开发中常用的PHP函数

标签:style   color   使用   strong   io   art   

原文地址:http://blog.csdn.net/szy361/article/details/38034407

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!