Utils.php
1.95 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
/**
 * Created by PhpStorm.
 * User: billy
 * Date: 12/02/2017
 * Time: 11:06 AM
 */
namespace App\Common;
class Utils
{
    /**
     * @return string
     */
    public static function guid()
    {
        $timestamp = time();
        $ranstr = rand(9999, 9999999999);
        return md5($timestamp . $ranstr) . substr(md5($ranstr), 0, 8);
    }
    /**
     * xml转为数组
     */
    public static function xmlToArray($xmlStr)
    {
        $msg = array();
        $postStr = $xmlStr;
        $msg = json_decode(json_encode(simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $msg;
    }
    /**
     * @param $number
     * @param int $precision
     * @return float|int
     */
    public static function round_up($number, $precision = 3)
    {
        $fig = (int) str_pad('1', $precision, '0');
        return (ceil($number * $fig) / $fig);
    }
    /**
     * @param $number
     * @param int $precision
     * @return float|int
     */
    public static function round_down($number, $precision = 3)
    {
        $fig = (int) str_pad('1', $precision, '0');
        return (floor($number * $fig) / $fig);
    }
    /**
     * @param $data
     * @param $key
     * @return mixed
     */
    public static function array_remove($data, $key)
    {
        if(!array_key_exists($key, $data)){
            return $data;
        }
        $keys = array_keys($data);
        $index = array_search($key, $keys);
        if($index !== FALSE){
            array_splice($data, $index, 1);
        }
        return $data;
    }
    /**
     * @param $day1
     * @param $day2
     * @return float|int
     */
    public static function diffBetweenTwoDays ($day1, $day2)
    {
        $second1 = strtotime($day1);
        $second2 = strtotime($day2);
        if ($second1 < $second2) {
            $tmp = $second2;
            $second2 = $second1;
            $second1 = $tmp;
        }
        return ($second1 - $second2) / 86400;
    }
}