zoukankan      html  css  js  c++  java
  • php取整函数、字符串运算函数、数组函数、时间函数

    一、取整函数

    1.floor();   向下取整,返回值为float类型;

    1 echo float(8.56);
    2 //print 8

    2.ceil();  向上取整,返回值为float类型;

    1 echo ceil(-3.2);2 //print -3

    3.round();  对浮点数进行四舍五入,返回值为float类型

    1 echo round(3.141592653);
    2 //print 3.14159265
    3 echo round(3.141592653, 2);
    4 //print 3.14

    4.intval();返回变量的整数值

    1 echo intval('899.3');
    2 //print 899
    3 echo (array('3'));
    4 print 1

    二、字符串函数

    1.strlen();返回给定字符串的长度

    attention:一个中文返回三个字符;

    2.mb_strlen($string, $encoding);获取字符串的长度

    1 #显示中文汉字的个数
    2 print_r(mb_strlen('陈健', 'gb2312'));
    3 // this will print 2

    3.mb_internal_encoding();显示当前的内部编码;
    4.strpos($str1, $str2, $offset);返回str2第一次在str1中出现的位置

    attention:字符串位置是从0开始,而不是从1开始;

    1 // 判断str1是否在str2中
    2 if (strpos($str2, $str1) !== false) {
    3     echo "this is a big family";        
    4 }
    5 
    6 echo strpos('abcdefg', 'b');
    7 // this will print 1;

    5.mb_strpos($str1, $str2, $offset, $encodeing)与strpos()最大的区别在于mb_strpos能处理中文;
    6.strrpos($str1, $str2, $offset);返回字符串str2最后一次出现在str1中的位置;

    1 echo strrpos('abcdefgnba', 'b');
    2 // this will print 8

    7.strtolower($str);将字符串转化为小写
    8.strtoupper($str);将字符串转化为大写

    三、数组函数

    1.array_change_key_case($arr, $case);返回字符串键名全为小写和大写的数组,$case为CASE_UPPER时返回大写, CASE_LOWER。

    1 $arr = array(
    2   'First' => 1,
    3   'SecOnd' => 4
    4 )
    5 
    6 print_r(array_change_key_case($arr, CASE_UPPER));
    7 
    8 // this will print array('FIRST'=>1, 'SECOND'=>4);

    2.array_combine($key, $value);数组$key和数组$value的值得个数应该相同
    3.array_count_values($input);统一数组中所有的值出现的个数,对于搜索一篇英文文章的单词出现频率很重要

    4.array_diff($arr1, $arr2);返回一个数组,里面的值在$arr1中存在,但在arr2中不存在。

    1 $arr1 = array('2','9','10');
    2 $arr2 = array('3', '6', '10');
    3 print_r(array_diff($arr1, $arr2));
    4 // this will print array([0]=>2, [1]=>9)

     四、时间函数(最后写一个时间转化函数)

    1.strtotime($time, $now);将任何英文文本的日期时间描述解析为 Unix 时间戳 ,$time参数参照日期与时间格式,$now(用来返回值的时间戳)如果为空的话默认当前时间;

     1 <?php
     2  echo  strtotime ( "now" ),  "
    " ;
     3 echo  strtotime ( "10 September 2000" ),  "
    " ;
     4 echo  strtotime ( "+1 day" ),  "
    " ;
     5 echo  strtotime ( "+1 week" ),  "
    " ;
     6 echo  strtotime ( "+1 week 2 days 4 hours 2 seconds" ),  "
    " ;
     7 echo  strtotime ( "next Thursday" ),  "
    " ;
     8 echo  strtotime ( "last Monday" ),  "
    " ;
     9  ?> 
    10 /*
    11 *this will print 
    12 *1412750036 
    13 *968515200
    14 *1412836436             // 一天的时间戳为86400           
    15 *1413354836 
    16 *81413542038 
    17 *1412784000 
    18 *1412524800
    19 */

    2.time();返回当前时间的unix时间戳;
    3.microtime();返回当前 Unix 时间戳和微秒数

     <?php echo microtime(); // this will print 0.88073800 1412750601 

    4.date($format, [$timestamp]);格式化本地时间/日期

    1 <?php
    2 echo  date('Y-m-d H:i:s')."
    ";
    3 // this will print 2014-10-08 15:07:15

    @时间转化函数

     1 function timeToString ($timestamp)
     2     {
     3         $date = array();
     4         $now = array();
     5         $timeString = date('Y/n/j/G/i/s', $timestamp);
     6         list($date['year'], $date['month'], $date['day'], $date['hour'], $date ['minute'], $date['second']) = explode('/', $timeString);
     7         list($now['year'], $now['month'], $now['day'], $now['hour'], $now['minute'], $now['second']) = explode('/', date('Y/n/j/G/i/s'));
     8         $date = array_map('intval', $date);
     9         $now = array_map('intval', $now);
    10 
    11         if ($date['year'] < $now['year']) {
    12             return date('Y-m-d', $timestamp);
    13         }
    14         if ($date['month'] < $now['month'] || $date['day'] + 2 < $now['day']) {
    15             return date('m-d', $timestamp);
    16         }
    17         if ($now['day'] - $date['day'] === 2) {
    18             return date('前天H:i', $timestamp);
    19         }
    20         if ($now['day'] - $date['day'] === 1) {
    21             return date('昨天H:i', $timestamp);
    22         }
    23         if ($date['hour'] < 12) {
    24             return date('上午h:i', $timestamp);
    25         }
    26         return date('下午h:i', $timestamp);
    27     }
  • 相关阅读:
    echarts的整理
    socket.io做一个简单的聊天案例
    WebSocket通信随笔
    接口返回的二维码图片,如何处理显示(axios处理后台返回图片流格式数据)
    小程序封装request请求
    一站式解决Vue插件开发,上传Github,Npm包发布
    微信公众号链接处理问题
    git常用命令
    vscode 常用配置
    项目中报错Cannot read property 'getAttribute' of undefined解决
  • 原文地址:https://www.cnblogs.com/eyeSpace/p/3896439.html
Copyright © 2011-2022 走看看