【问题标题】:strtotime returning false datestrtotime 返回错误日期
【发布时间】:2013-10-25 10:17:23
【问题描述】:

我在 php 中的 strtotime 函数有问题,我正在尝试将 mm-dd-yyyy 转换为 yyyy-mm-dd。在文本框中输入日期,提交时应将日期保存到数据库中。问题是它每次都返回一个错误的日期(1970-01-01),这意味着我的代码没有使用我用来存储日期的变量,我的代码是:

//dateconvert
$submitdate = date($_POST['date']);
$date   = date("Y-m-d", strtotime($submitdate));

//storeindb
$query ="INSERT INTO ticket SET date = '$date'";
$result = mysql_query($query);

我是新手,请帮忙

【问题讨论】:

  • mm-dd-yyyy 不是strtotime 可以翻译的格式,它应该是mm/dd/yyyy(解释为美国日期)或dd-mm-yyyy(解释为欧洲日期,或至少是意大利日期)
  • $_POST['date'] 的值是多少?
  • @MatteoTassinari 我用过 mm/dd/yyyy 但它仍然返回相同的值
  • 实际发布的价值是多少?
  • @AcyclicTau 它采用文本框中输入的日期(值:2013-10-25)

标签: php date strtotime


【解决方案1】:

Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.

http://php.net/manual/en/function.strtotime.php

对于 mm/dd/yyyy 格式,您需要使用正斜杠分隔符。

【讨论】:

    【解决方案2】:

    将代码用作:

    $submitdate = $_POST['date'];
    $date = date("Y-m-d", strtotime($submitdate));
    

    希望有帮助。

    【讨论】:

      【解决方案3】:

      $submitdate 中的日期格式不正确因为格式 yyyy/mm/dd 应为 yyyy-mm-dd,因此您需要替换您的 / 字符。

      试试这个:

      $submitdate = str_replace("/","-",$_POST['date']);
      echo date('Y-m-d', strtotime($submitdate));
      

      【讨论】:

      • 这个我之前用过...不起作用...返回与以前相同的值
      • 那么我猜你的日期必须是dd-mm-yyyy 格式。如果是这样,请将其更改为yyyy-mm-dd 格式。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-19
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 2012-02-21
      相关资源
      最近更新 更多