• PHP字符串处理和时间格式化整理


    一、PHP字符串相关

    1、字符串截取(开始位置、长度)

    echo substr("Hello world",0,10)."<br>";      //Hello worl
    echo substr("Hello world",0,-1)."<br>";      //Hello worl
    echo substr("Hello world",1)."<br>";          //ello world

    2、查看字符串是否包含

    if(strpos('www.jb51.net','jb51') !== false){
    echo '包含jb51';                              //包含jb51
    }else{
    echo '不包含jb51';
    }

    3、字符串转换(查找的值、替换的值、原字符串)

    echo str_replace("world","Shanghai","Hello world!");      //Hello Shanghai!

    4、字符串长度

    echo strlen('asadasda'); //8

    5、根据特定字符分割成数组

    $arrlist = explode(',', 'hello,world');
    foreach ($arrlist as $arr)
    {
    echo $arr;              //[0]hello [1]world
    }

    6、将数组组成字符串

    $test = array("hello","world","php");
    echo implode("-",$test);       //hello-world-php

    7、判断变量是否为空

    $a = 0;||$a = '';||$a = array();||$a=false      //0或''或空数组或false都是空
    if (empty($b))
    {
    echo '$a 为空';
    }

    8、检查变量是否设置

    $a = null; //null或不存在
    echo isset($a); // FALSE
    echo isset($b); // FALSE

    二、PHP日期格式

    1、时间格式化

    echo time();                  //当前时间戳 1542849318

    echo strtotime("+1 day");             //明天这时候时间戳 1542935932

    echo date("Y-m-d")                 //当前年月日 2018-11-22

    echo date("Y-m-d H:i:s",time())         //当前时间 2018-11-22 09:14:15

    date("Y-m-d",strtotime("-1 day"))                           //昨天日期

    echo date('Y');                //当前年份 2018

    echo floor((strtotime("2018-12-14")-strtotime("2018-12-04"))/86400)      //计算日期相差天数

    2、格式转换

    $time_str = date('Y-m-d H:i:s', time());       // 将时间戳转化为相应的时间字符串
    echo $time_str;                // string(19) "2018-01-17 02:24:34"
    $time_int = strtotime($time_str);          // 将时间字符串转化为时间戳
    echo $time_int;                // int(1516155874)

  • 相关阅读:
    POJ 2411 Mondriaan's Dream -- 状压DP
    codeforces 792A-D
    codeforces 796A-D
    Acdream1201 SuSu's Power
    HDU 2818 Building Block
    C# NetStream
    基于Duff's Device的C简易无栈协程实现
    CentOS 多版本 GCC 共存
    2017杭电多校第一场
    2019杭电多校第十场
  • 原文地址:https://www.cnblogs.com/qiujianfeng/p/9999319.html
Copyright © 2020-2023  润新知