colly_wyx
2018-04-26 3fffc1da75d49f832b9faf3467d91202e0220a3b
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
<?php
/**
 * PhalApi_Request_Formatter_Date 格式化日期
 *
 * @package     PhalApi\Request
 * @license     http://www.phalapi.net/license GPL 协议
 * @link        http://www.phalapi.net/
 * @author      dogstar <chanzonghuang@gmail.com> 2015-11-07
 */
 
class PhalApi_Request_Formatter_Date extends PhalApi_Request_Formatter_Base implements PhalApi_Request_Formatter {
 
    /**
     * 对日期进行格式化
     *
     * @param timestamp $value 变量值
     * @param array $rule array('format' => 'timestamp', 'min' => '最小值', 'max' => '最大值')
     * @return timesatmp/string 格式化后的变量
     *
     */
    public function parse($value, $rule) {
        $rs = $value;
 
        $ruleFormat = !empty($rule['format']) ? strtolower($rule['format']) : '';
        if ($ruleFormat == 'timestamp') {
            $rs = strtotime($value);
            if ($rs <= 0) {
                $rs = 0;
            }
 
            if (isset($rule['min']) && !is_numeric($rule['min'])) {
                $rule['min'] = strtotime($rule['min']);
            }
            if (isset($rule['max']) && !is_numeric($rule['max'])) {
                $rule['max'] = strtotime($rule['max']);
            }
 
            $rs = $this->filterByRange($rs, $rule);
        }
 
        return $rs;
    }
}