【问题标题】:PHP - Changing the datetime to mysql readable datetime [closed]PHP - 将日期时间更改为 mysql 可读的日期时间 [关闭]
【发布时间】:2013-08-09 21:30:23
【问题描述】:

我的日期和时间字符串是:January 28, 2010 1417,最后四个数字是时间。 我应该如何将它转换为mysql可以接受的字符串?

当我尝试使用 strtotime 时,我得到 Warning: strtotime(): It is not safe to rely on the system's timezone settings.

【问题讨论】:

  • 要求代码的问题必须表明对正在解决的问题有最低限度的了解。包括尝试的解决方案、它们为什么不起作用以及预期结果。另见:Stack Overflow question checklist

标签: php mysql datetime


【解决方案1】:
$timestamp = strtotime($string);


date("Y-m-d H:i:s", $timestamp);

【讨论】:

  • 这会弄乱时间,但它会正确获取日期。我也收到警告:strtotime():依赖系统的时区设置是不安全的。
  • @MarlonBrando php.net/manual/en/function.strtotime.php 请参阅此处的错误/异常。您的 php 版本是什么?
【解决方案2】:
DateTime::createFromFormat($timeString, 'F d, Y Hi')->format('Y-m-d H:i:s');

(您可能需要弄清楚您的时间字符串是否在月份中使用前导零:“1 月 5 日”还是“1 月 5 日”?)

【讨论】:

    【解决方案3】:

    根据您的编程语言,尝试将您的日期/时间字符串转换为单引号内的内容,格式如下:

    'YYYY-MM-DD HH:MM:SS'
    

    更多信息请参见this post

    【讨论】:

      【解决方案4】:
      $d = new DateTime('January 28, 2010 1417');
      echo $d->format('Y-m-d h:i:s');
      

      这打印2010-01-28 14:17:00

      【讨论】:

        猜你喜欢
        • 2019-12-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-16
        • 2012-04-09
        • 2014-04-12
        • 2011-07-09
        相关资源
        最近更新 更多