PHP进行日期计算

PHP需要进行日期计算的时候,可以使用PHP自带的date_diff进行计算操作,而无需进行时间戳转换等复杂操作

<?php 
$date_expire = '2021-03-07 23:59:59';
$date_now = date("Y-m-d G:i:s",time());
$date = new DateTime($date_expire);
$now = new DateTime($date_now);
$diff = date_diff($date,$now);

echo $diff->format(' %a 日 %h 小时 %i 分 %s 秒"');
?>

使用FORMAT是为了格式化一下输出的时间

PHP date_interval_format() 函数参数

参数描述
format

必需。规定格式。format 参数字符串可以使用下列的字符:

  • % – Literal %
  • Y – 年,至少 2 个数字,带前导零(例如 03)
  • y – 年(例如 3)
  • M – 月,带前导零(例如 06)
  • m – 月(例如 6)
  • D – 日,带前导零(例如 09)
  • d – 日(例如 9)
  • a – 由 date_diff() 得出的两个日期间隔的总天数
  • H – 小时,带前导零(例如 08、23)
  • h – 小时(例如 8、23)
  • I – 分,带前导零(例如 08、23)
  • i – 分(例如 8、23)
  • S – 秒,带前导零(例如 08、23)
  • s – 秒(例如 8、23)
  • R – 当负数时为符号 “-“,当正数时为符号 “+”
  • r – 当负数时为符号 “-“,当正数时为空

注释:每个格式字符串必须以 % 符号作为前缀!

laravel更改public目录名(去掉地址里的public)

话说今天有一个前同事问我初接触laravel框架,发现地址栏里有public名,怎么去除。

其实度娘或者谷歌一下就可以得到一个简单的解决办法(修改server.php文件),但实际上laravel(我的前同事就经常给打成lavarel)的结构是尽量避免其他系统文件可以被直接使用URL访问,那么正确的放置方式是,除了PUBLIC目录内的文件上传至环境主机WEB目录之外,其他都放置在与WEB根目录同级的位置。

我的这位前同事使用的是AMH框架,只需要将内容使用SSH上传或者给WEB之前的主标志域名一级目录给一个775甚至777权限,上传完毕再改回来即可

/home/wwwroot/环境名/domain/主标志域名/

具体LINUX目录权限操作命令可自行搜索。

至于有人问我没有VPS只有一个虚拟主机,个人建议还是放弃折腾框架吧,用个现成的CMS或者BLOG不好么?

 

判断移动设备的function isMobile()(PHP)

不废话上代码,使用方法就是


function isMobile() {

$user_agent = $_SERVER['HTTP_USER_AGENT'];
$mobile_agents = Array("240x320", "acer", "acoon", "acs-", "abacho", "ahong", "airness", "alcatel", "amoi", "android", "anywhereyougo.com", "applewebkit/525", "applewebkit/532", "asus", "audio", "au-mic", "avantogo", "becker", "benq", "bilbo", "bird", "blackberry", "blazer", "bleu", "cdm-", "compal", "coolpad", "danger", "dbtel", "dopod", "elaine", "eric", "etouch", "fly ", "fly_", "fly-", "go.web", "goodaccess", "gradiente", "grundig", "haier", "hedy", "hitachi", "htc", "huawei", "hutchison", "inno", "ipad", "ipaq", "ipod", "jbrowser", "kddi", "kgt", "kwc", "lenovo", "lg ", "lg2", "lg3", "lg4", "lg5", "lg7", "lg8", "lg9", "lg-", "lge-", "lge9", "longcos", "maemo", "mercator", "meridian", "micromax", "midp", "mini", "mitsu", "mmm", "mmp", "mobi", "mot-", "moto", "nec-", "netfront", "newgen", "nexian", "nf-browser", "nintendo", "nitro", "nokia", "nook", "novarra", "obigo", "palm", "panasonic", "pantech", "philips", "phone", "pg-", "playstation", "pocket", "pt-", "qc-", "qtek", "rover", "sagem", "sama", "samu", "sanyo", "samsung", "sch-", "scooter", "sec-", "sendo", "sgh-", "sharp", "siemens", "sie-", "softbank", "sony", "spice", "sprint", "spv", "symbian", "tablet", "talkabout", "tcl-", "teleca", "telit", "tianyu", "tim-", "toshiba", "tsm", "up.browser", "utec", "utstar", "verykool", "virgin", "vk-", "voda", "voxtel", "vx", "wap", "wellco", "wig browser", "wii", "windows ce", "wireless", "xda", "xde", "zte");

$is_mobile = false;
foreach ($mobile_agents as $device) {
  if (stristr($user_agent, $device)) {
    $is_mobile = true;
    break;
  }
}
return $is_mobile;
}

一个PHP裁切字符的function

主要应用在THINKPHP的模板上

function msubpost($str, $start=0, $length=18, $charset="utf-8", $suffix=false) {
  if(strlen($str) > ($length*2)){
    $suffix = true;
  }else{
    $suffix = false;
  }
  if(function_exists('iconv_substr') || $charset=="utf-8") {
    $slice = iconv_substr($str,$start,$length,$charset);
    if(false === $slice) {
        $slice = '';
    }
  }elseif(function_exists("mb_substr")){
    $slice = mb_substr($str, $start, $length, $charset);
  }else{
    $re['utf-8']   = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
    $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
    $re['gbk']    = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
    $re['big5']   = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
    preg_match_all($re[$charset], $str, $match);
    $slice = join("",array_slice($match[0], $start, $length));
  }
  return $suffix ? $slice.'...' : $slice;
}

WDCP升级PHP和MYSQL

WDCP作为中国人自己的VPS控制面板,整体上来说非常不错,但默认的PHP版本是5.2.17,MySQL是5.1.63,实在有点低,甚至低到了部分PHP框架和程序都无法运行(例如本站使用的wordpress),所以必须要升级才行。下面就分步骤简述升级流程

升级php 5.3

登录SSH,按顺序执行下面的命令

wget http://down.wdlinux.cn/in/php_up53.sh
sh php_up53.sh

等待屏幕上停止代码滚动,并提示php update is OK就表示升级成功了,可以使用PHP INFO看一下版本。
WDCP升级PHP和MYSQL