您现在的位置是:首页 > 其他

李清波 2017-03-27 其他 3115 复制当前网址

PHP自定义时间格式化函数time_format,mydate


时间戳格式化

/**
 * 时间戳格式化
 * @param int $time
 * @return string 完整的时间显示
 * @author huajie <banhuajie@163.com>
 */
function time_format($time = NULL,$format='Y-m-d H:i:s'){
    $time = $time === NULL ? NOW_TIME : intval($time);
    return date($format, $time);
}

讲解:

time参数格式为时间戳

format参数为格式化后的时间格式

当传入的时间戳参数为空的时候,默认将当前电脑的时间格式化,格式为format传入的格式,空为默认



固定式:

function mydate($time=''){
    if(!empty($time)) return date('Y-m-d H:i:s',$time);
    return '';
}

讲解

当参数时间戳不为空的时候将时间格式化为:年-月-日 小时-分钟-秒

如果参数为空的时候也返回空值(防止参数空的情况下返回错误时间)


文章来源:http://liqingbo.com/blog-1332.html

评论