douyou9923
douyou9923
2009-05-19 01:58

格式化日期时出现问题

已采纳

I am trying to write a function to format a date and time for me. I have an almost identical function which formats just a date. That function works fine. I just added some code to try and have it format the date with a time. It should return something like "May 18, 2009 9:50 PM" but I am getting this warning:

Warning: mktime() expects parameter 6 to be long, string given in
public_html/include/functions.php on line 421

Here is the code I have:

function dateTimeFormat($dateIn)
{
   $x = explode(" ",$dateIn);
   $y = explode("-",$x[0]);
   $z = explode(":",$x[1]);

   $year = $y[0]; 
   $month = $y[1];
   $day = $y[2];
   $hour = $z[0];
   $min = $z[1];

   $dateOut =date("F j, Y h:i A", mktime($hour, $min, 0, $month, $day, $year)); 

   return $dateOut;
}

What it is putting out is wrong too. It puts out:

December 31, 1969 07:00 PM

but the timestamp in the database is

2009-05-18 05:07:39
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dtcyv3985 dtcyv3985 12年前

    PHP already has a perfectly good date-parsing function: strtotime(). It returns a Unix timestamp which you can pass to date().

    In other words, your function can be reduced to this:

    function dateTimeFormat($dateIn)
    {
        return date("F j, Y h:i A", strtotime($dateIn));
    }
    
    点赞 评论 复制链接分享