PHP的日期时间格式化函数

函数名:date_format
参数: $string 时间源,可以是2006-04-24 09:56:07这种格式,$format要格式化的形式,如%Y年%m月%d日%H时%M分%S秒看需要删改
示例:<?php
echo date_format($rs['time'],'%Y年%m月%d日%H时%M分%S秒');
?>


function date_format($string, $format="%b %e, %Y", $default_date=null)
{
    if (substr(PHP_OS,0,3) == 'WIN') {
           $_win_from = array ('%e',  '%T',       '%D');
           $_win_to   = array ('%#d', '%H:%M:%S', '%m/%d/%y');
           $format = str_replace($_win_from, $_win_to, $format);
    }
    if($string != '') {
        return strftime($format, smarty_make_timestamp($string));
    } elseif (isset($default_date) && $default_date != '') {
        return strftime($format, smarty_make_timestamp($default_date));
    } else {
        return;
    }
}

function smarty_make_timestamp($string)
{
    if(empty($string)) {
        $string = "now";
    }
    $time = strtotime($string);
    if (is_numeric($time) && $time != -1)
        return $time;

    // is mysql timestamp format of YYYYMMDDHHMMSS?
    if (PReg_match('/^\d{14}$/', $string)) {
        $time = mktime(substr($string,8,2),substr($string,10,2),substr($string,12,2),
               substr($string,4,2),substr($string,6,2),substr($string,0,4));

        return $time;
    }

    // couldn't recognize it, try to return a time
    $time = (int) $string;
    if ($time > 0)
        return $time;
    else
        return time();
}

关键词: php

上一篇: Discuz! 5 SQL injection Exploit
下一篇: Sebug.net正式版 上线

相关文章
目前还没有人评论,您发表点看法?
发表评论

评论内容 (必填):