dongshengli6384
2012-07-30 05:29
浏览 215
已采纳

结果在UNIX时间戳中插入mysql数据库

I have a strange problem . I am trying to insert some values to database with respective date . In HTML form, the date format is mm/dd/yyyy format. I converted this format to mysql yyyy-mm-dd format using the following PHP (from stackoverflow answer):

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

But the above echo , when I run the code it shows like this: 07-30-2012 1970-01-01

图片转代码服务由CSDN问答提供 功能建议

我有一个奇怪的问题。 我试图将一些值插入到具有相应日期的数据库中。 在HTML表单中,日期格式为mm / dd / yyyy格式。 我使用以下PHP将此格式转换为mysql yyyy-mm-dd 格式(来自stackoverflow答案)

  echo $ date1 = str_replace(”/“,” - “,$ _ POST ['date]  ']); 
echo $ date = date('Ym-d',strtotime($ date1)); 
   
 
 

但是上面的回声,当我运行 代码如下所示: 07-30-2012 1970-01-01

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • drevls8138 2012-07-30 05:36
    已采纳
    echo $date = preg_replace('/^(\d\d)\/(\d\d)\/(\d{4})$/', '$3-$1-$2', $_POST['date']);
    

    I think this code should work fine, but of course you must do all necessary checks before insert it into database.

    点赞 打赏 评论
  • douzou0073 2012-07-30 05:34

    I've just tried :

    $date = "30/07/2012";
    echo $date1 = str_replace("/","-",$date);
    echo '<br />';
    echo $date = date('Y-m-d', strtotime($date1));
    

    And it's actually returning :

    30-07-2012
    2012-07-30
    

    You should check your $_POST['date'] format.

    点赞 打赏 评论
  • dsfsw1233 2012-07-30 05:38
    echo $date1 = str_replace("/","-","$_POST['date']");
    echo $date = date('Y-m-d', strtotime($date1));
    

    put double quotes in date then you get perfect result

    点赞 打赏 评论
  • doushui20090526 2012-07-30 05:39

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

    Your first line is incorrect, which returns false, which (converted to an integer) is 0, 0 is the beginning of time on a linux machine! (hence 1970-01-01)

    点赞 打赏 评论
  • dongweng6241 2012-07-30 05:40

    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. See http://php.net/manual/en/function.strtotime.php for details.

    So even you are passing '07-30-2012' as an input, it is considering 07 as date, 30 as month hence you are getting incorrect results.

    Following should work

    echo $date = date('Y-m-d', strtotime($_POST['date']));
    
    点赞 打赏 评论
  • doutou6803 2012-07-30 05:40

    Your input is in mm/dd/yyyy format, that means you should use slashes / You would need to change to dashes if you were using dd-mm-yyyy format, as is shown in the answer you link to. So in your case you should not replace the /.

    点赞 打赏 评论

相关推荐 更多相似问题