Mr.Blog


  • 首页

  • 归档

  • 标签

  • 分类

  • 搜索

PHP 常用方法 (一)

发表于 2018-03-20 | 分类于 PHP

获取当前时间,精确到微秒

1
2
3
4
5
6
7
8
9
10
/**
* 获取当前时间,精确到微秒
*
* @return string
*/
function getCurrentTime()
{
list($usec, $sec) = explode(' ', microtime());
return date('Y-m-d H:i:s', $sec) . substr((float)$usec, 1);
}

递归创建目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* 递归创建目录
*
* @param string $dir
* @param int $mode

* @return bool
*/
function recursive_mkdirs($dir, $mode = 0777)
{
if (is_dir($dir) || @mkdir($dir, $mode)) {
return true;
}

if (!mkdirs(dirname($dir), $mode)) {
return false;
}

return @mkdir($dir, $mode);
}
阅读全文 »

坐标常用函数

发表于 2018-03-10 | 分类于 PHP

百度/高德计算两经纬度坐标的距离和某个经纬度坐标的周边

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* 计算两点之前距离
*
* @param string $long1 出发地经度
* @param string $lat1 出发地纬度
* @param string $long2 目的地经度
* @param string $lat2 目的地纬度
* @param int $type 1:公里,2:米
*
* @return int
*/
function getLatLngDistance($long1, $lat1, $long2, $lat2, $type = 1)
{
$PI = 3.1415926535898;
$EARTH_RADIUS = 6378.137;
$radLat1 = $lat1 * ($PI / 180);
$radLat2 = $lat2 * ($PI / 180);
$a = $radLat1 - $radLat2;
$b = ($long1 * ($PI / 180)) - ($long2 * ($PI / 180));
$s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
$s = $s * $EARTH_RADIUS;
$s = round($s * 1000);

return $type == 1 ? ($s / 1000) : $s;
}

/**
* 计算范围
*
* @param string $long 中心点经度
* @param string $lat 中心点纬度
* @param double $radius 范围[单位:米]
*/
function getLatLngRange($long, $lat, $radius)
{
$PI = 3.1415926535898;
// 计算纬度
$degree = (24901 * 1609) / 360.0;
$dpmLat = 1 / $degree;
$radiusLat = $dpmLat * $radius;
$minLat = $lat - $radiusLat; // 得到最小纬度
$maxLat = $lat + $radiusLat; // 得到最大纬度
// 计算经度
$mpdLong = $degree * cos($lat * ($PI / 180));
$dpmLong = 1 / $mpdLong;
$radiusLong = $dpmLong * $radius;
$minLong = $long - $radiusLong; // 得到最小经度
$maxLong = $long + $radiusLong; // 得到最大经度
// 范围
$range = [
'minLat' => $minLat,
'maxLat' => $maxLat,
'minLong' => $minLong,
'maxLong' => $maxLong
];

return $range;
}
阅读全文 »

微信小程序 - websocket wss

发表于 2018-03-07 | 分类于 PHP

准备工作:
1、GatewayWorker 监听 8585 端口(websocket协议)
2、已经申请了ssl证书[阿里云免费证书], 放在了/server/httpd/cert/ 下
3、利用apache转发443端口至指定端口
4、httpd-ssl.conf 已加载
5、openssl 已安装
6、小程序已设置 socket 合法域名 wss://www.xxx.com

如果处于开发阶段,关闭小程序的相关校验, 微信web开发者工具 -> 设置 -> 项目设置 -> 勾选 不校验安全域名、web-view 域名、TLS 版本以及 HTTPS 证书

启用 proxy_wstunnel_module 模块

1
2
3
#httpd.conf
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so
阅读全文 »

Symfony3 Doctrine2 自定义 DQL 函数

发表于 2018-03-01 | 分类于 Symfony

在 Symfony 中使用 mysql 函数时,有些函数需要注册自定义 DQL 函数来实现,否则无法使用
自定义 DQL 函数时需要查看的两个文件:
verdor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php
verdor/doctrine/orm/lib/Doctrine/ORM/Query/Lexer.php

需要实现的三个函数, DATE_FORMAT, GLength, If

1
2
3
4
5
6
7
8
9
10
11
# app/config/config.yml
# Doctrine Configuration
doctrine:
orm:
dql:
datetime_functions:
DATE_FORMAT: Bundles\CommonBundle\DQL\DateFormatFunction
numeric_functions:
GLength: Bundles\CommonBundle\DQL\GLengthFunction
string_functions:
If: Bundles\CommonBundle\DQL\IfFunction

datetime_functions、numeric_functions、string_functions 是固定的,必须按此命名
DateFormat、GLength、If 命名可以自定义,但是SQL调用的时候,必须与这里定义的一致

阅读全文 »

PHP 系列 (一)

发表于 2018-02-24 | 分类于 PHP

浮点数计算不准确|保留两位小数不四舍五入

1
2
3
4
5
6
7
8
9
// 结果 float(0.099999999999998) 
var_dump(19.45 - 19.35);

// 设置所有bc数学函数的小数点保留位数
bcscale(2);
var_dump(bcsub(19.45, 19.35));

// 保留两位小数,不四舍五入
var_dump(bcsub(1.339, 0, 2));

DateTime 获取当前时间, 以微秒为单位

1
2
3
4
5
// 输出当前时间, 以微秒为单位, 若直接将 $timeZone 作为 createFromFormat 的第三个参数是无效的, 问题是 microtime() 和 time()  返回的是当前时区的时间戳
// 参考 http://php.net/manual/zh/datetime.createfromformat.php#120552
$timeZone = new DateTimeZone(date_default_timezone_get() ? : 'Asia/Shanghai');
$ds = DateTime::createFromFormat('U.u', microtime(true))->setTimezone($timeZone);
var_dump($ds->format('Y-m-d H:i:s.u'));

删除不可见字符与BOM头

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 删除ASCII码 0-31,127-255 字符
$string = preg_replace('/[\x00-\x1F\x7F-\xFF]/', '', $string);

// 删除ASCII码 0-31,127 字符
$string = preg_replace('/[\x00-\x1F\x7F]/', '', $string);

// 删除BOM,BOM的ASCII码为 239,187,191
$string = trim($string, "\xEF\xBB\xBF");

// IOS提交的不可见字符
$string = "‭1234567890‬";
var_dump(json_encode([$string]));
$string = preg_replace('/[\x7F-\xFF]/', '', $string);
var_dump(json_encode([$string]));
阅读全文 »

PHP 全局使用 Laravel 辅助函数 dd

发表于 2018-02-11 | 分类于 PHP

dump() 方法

全局 composer.json
1
2
3
4
5
"require": {
"squizlabs/php_codesniffer": "*",
"fxp/composer-asset-plugin": "^1.4",
"symfony/var-dumper": "3.3.16"
}

配置PHP.ini

1
auto_prepend_file = "C:\Users\MS\AppData\Roaming\Composer\vendor\autoload.php"

更新Composer

1
composer global update

更新后重启apache就可以全局使用函数 dump()

阅读全文 »

MySQL 主从配置

发表于 2018-02-07 | 分类于 MySQL
操作系统 mysql版本 IP 主从关系 简称
Win8.1-64位 5.7.14 192.168.50.87 主【Master】 Master
虚拟机【Centos7 64位】 5.7.18 192.168.227.128 从【Slave】 Slave

Master 配置

  1. 配置 my.ini, 配置后需重启

    1
    2
    3
    4
    5
    6
    [mysqld]
    server-id=1 #标识唯一的数据库,这里设置为1,在设置从库的时候就需要设置为其他值
    log-bin=mysql-bin #打开日志
    binlog-ignore-db=mysql #指定不需要同步的数据库[可以添加多条binlog-ignore-db命令]
    binlog-do-db=test #指定需要同步的数据库[可以添加多条binlog-do-db命令]
    #expire_log_days=1 #自动清理1天前的log文件
  2. 创建从库权限账号 test, 密码 test,设置 File、REPLICATION SALVE 权限, 并只允许来源地址为 192.168.227.128

    1
    2
    GRANT FILE,REPLICATION SLAVE ON *.* TO 'test'@'192.168.50.82' IDENTIFIED BY 'test';
    FLUSH PRIVILEGES;
  3. 查看主库信息,这里的 File, Position 在配置 Slave 时会用到

    1
    2
    3
    4
    5
    6
    7
    mysql> show master status;
    +------------------+----------+---------------+------------------+-------------------+
    | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
    +------------------+----------+---------------+------------------+-------------------+
    | mysql-bin.000001 | 154 | test | mysql | |
    +------------------+----------+---------------+------------------+-------------------+
    1 row in set (0.20 sec)
阅读全文 »
1…12131415
Mr

Mr

懒........

102 日志
21 分类
56 标签
RSS
Links
  • Novnan
  • 挨踢Blog
  • Eagle
  • Timber
  • 谢炳南
© 2020 — 2021 Mr
0%