douyou9923 2009-05-19 01:58
浏览 169
已采纳

格式化日期时出现问题

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 2009-05-19 02:05
    关注

    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));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?