php 中常用的日期处理函数

  1. // date_format2($rs['time'],'%y年%m月%d日%h时%m分%s秒');
  2. function date_format2($string, $format='%b %e, %y', $default_date=null)
  3. {
  4. if (substr(php教程_os,0,3) == 'win') {
  5. $_win_from = array ('%e', '%t', '%d');
  6. $_win_to = array ('%#d', '%h:%m:%s', '%m/%d/%y');
  7. $format = str_replace($_win_from, $_win_to, $format);
  8. }
  9. if($string != '') {
  10. return strftime($format, smarty_make_timestamp($string));
  11. } elseif (isset($default_date) && $default_date != '') {
  12. return strftime($format, smarty_make_timestamp($default_date));
  13. } else {
  14. return;
  15. }
  16. }
  17. function smarty_make_timestamp($string){
  18. if(emptyempty($string)) {
  19. $string = "now";
  20. }
  21. $time = strtotime($string);
  22. if (is_numeric($time) && $time != -1)
  23. return $time;
  24. if (preg_match('/^d{14}$/', $string)) {
  25. $time = mktime(substr($string,8,2),substr($string,10,2),substr($string,12,2),
  26. substr($string,4,2),substr($string,6,2),substr($string,0,4));
  27. return $time;
  28. }
  29. $time = (int) $string;
  30. if ($time > 0)
  31. return $time;
  32. else
  33. return time();
  34. }