<?php
/**
 * Link         :   http://www.phpcorner.net
 * User         :   qingbing<780042175@qq.com>
 * Date         :   2018-10-27
 * Version      :   1.0
 */

namespace Helper;

/**
 * 常用格式化
 * Class Format
 * @package Helper
 */
class Format
{
    /**
     * 返回数组的值
     * @param string $key
     * @param array $data
     * @return mixed
     */
    static public function dataValue($key, $data = [])
    {
        if (isset($data[$key])) {
            return $data[$key];
        }
        return $key;
    }

    /**
     * 标准格式化日期
     * @param null|int $time
     * @param bool $todayIfNull
     * @param string $format
     * @return null|string
     */
    public static function date($time = null, $todayIfNull = true, $format = 'Y-m-d')
    {
        if (empty($time)) {
            return $todayIfNull ? date($format) : null;
        } else {
            return date($format, $time);
        }
    }

    /**
     * 标准格式化日期
     * @param null|int $time
     * @param bool $nowIfNull
     * @param string $format
     * @return null|string
     */
    public static function datetime($time = null, $nowIfNull = true, $format = 'Y-m-d H:i:s')
    {
        if (empty($time)) {
            return $nowIfNull ? date($format) : null;
        } else {
            return date($format, $time);
        }
    }
}