从数据库里查询出的时间是字符串:15/08/2009 12:44,在ASP中可以用year,month,day等函数解析出来,不知道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();
参数: $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();
温馨提示:答案为网友推荐,仅供参考
第1个回答 推荐于2017-06-30
<?php
// 设定要用的默认时区。自 PHP 5.1 可用
date_default_timezone_set('UTC');
// 输出类似:Monday
echo date("l");
// 输出类似:Monday 15th of August 2005 03:12:46 PM
echo date('l dS \of F Y h:i:s A');
// 输出:July 1, 2000 is on a Saturday
echo "July 1, 2000 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2000));
/* 在格式参数中使用常量 */
// 输出类似:Mon, 15 Aug 2005 15:12:46 UTC
echo date(DATE_RFC822);
// 输出类似:2000-07-01T00:00:00+00:00
echo date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000));
//打印出当前日期
echo date('Y年m月d日');
//格式化已有时间
echo date('Y年m月d日', strtotime('1986-12-05'));
/*
输出
Monday
Monday 16th of April 2012 02:04:36 PM
July 1, 2000 is on a Saturday
Mon, 16 Apr 12 14:04:36 +0000
2000-07-01T00:00:00+00:00
2012年04月16日
*/
?>本回答被网友采纳
// 设定要用的默认时区。自 PHP 5.1 可用
date_default_timezone_set('UTC');
// 输出类似:Monday
echo date("l");
// 输出类似:Monday 15th of August 2005 03:12:46 PM
echo date('l dS \of F Y h:i:s A');
// 输出:July 1, 2000 is on a Saturday
echo "July 1, 2000 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2000));
/* 在格式参数中使用常量 */
// 输出类似:Mon, 15 Aug 2005 15:12:46 UTC
echo date(DATE_RFC822);
// 输出类似:2000-07-01T00:00:00+00:00
echo date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000));
//打印出当前日期
echo date('Y年m月d日');
//格式化已有时间
echo date('Y年m月d日', strtotime('1986-12-05'));
/*
输出
Monday
Monday 16th of April 2012 02:04:36 PM
July 1, 2000 is on a Saturday
Mon, 16 Apr 12 14:04:36 +0000
2000-07-01T00:00:00+00:00
2012年04月16日
*/
?>本回答被网友采纳
第2个回答 2017-06-29
strtotime(你查出来的字符串);