简体   繁体   English

日期在mysql中插入as 0000-00-00 00:00:00

[英]Date is inserting as 0000-00-00 00:00:00 in mysql

My $date output is in the foreach loop 我的$date输出是在foreach循环中

09/25/11, 02/13/11, 09/15/10, 06/11/10, 04/13/10, 04/13/10, 04/13/10, 09/24/09, 02/19/09, 12/21/08 09/25/11,02/13/11,09/15 / 10,06 / 11 / 10,04 / 13 / 10,44 / 13 / 10,04 / 13 / 10,09 / 24 / 09,22 / 19/09年12月21日

My mysql query(PHP) is as follows 我的mysql查询(PHP)如下

("INSERT INTO table_name(`field1`, `field2`,`date`) VALUES ('".$value1."','".$value2 ."','".$date."')");

Question: In my database all the dates stores as 0000-00-00 00:00:00 . 问题:在我的数据库中,所有日期都存储为0000-00-00 00:00:00 But 4th date (06/11/10) is stored as 2006-11-10 00:00:00 . 但是第4个日期(06/11/10)被存储为2006-11-10 00:00:00

I tried with date('Ymd H:i:s', $date); 我试过date('Ymd H:i:s', $date); but no help. 但没有帮助。

Note: My database field is datetime type. 注意:我的数据库字段是日期时间类型。 Any idea? 任何想法?

You're on the right track with your date('Ymd H:i:s',$date); 你的date('Ymd H:i:s',$date);正确date('Ymd H:i:s',$date); solution, but the date() function takes a timestamp as its second argument, not a date. 解决方案,但date()函数将时间戳作为其第二个参数,而不是日期。

I'm assuming your examples are in American date format, as they look that way. 我假设您的示例采用美国日期格式,因为它们看起来就是这样。 You can do this, and it should get you the values you're looking for: 你可以做到这一点,它应该为你提供你正在寻找的价值观:

date('Y-m-d H:i:s', strtotime($date));

The reason it's not working is because it expects the date in the YYYY-MM-DD format, and tries to evaluate your data as that. 它不起作用的原因是因为它期望YYYY-MM-DD格式的日期,并尝试评估您的数据。 But you have MM/DD/YY, which confuses it. 但你有MM / DD / YY,这让人感到困惑。 The 06/11/10 example is the only one that can be interpreted as a valid YYYY-MM-DD date out of your examples, but PHP thinks you mean 06 as the year, 11 as the month, and 10 as the day. 06/11/10示例是唯一一个可以被解释为您的示例中的有效YYYY-MM-DD日期的示例,但PHP认为您的意思是06作为年份,11表示月份,10表示当天。

I created my own function for this purpose, may be helpful to you: 我为此目的创建了自己的函数,可能对您有所帮助:

function getTimeForMysql($fromDate, $format = "d.m.y", $hms = null){
    if (!is_string($fromDate))  
        return null ;       
    try {
        $DT = DateTime::createFromFormat($format, trim($fromDate)) ;
    } catch (Exception $e) { return null ;}

    if ($DT instanceof DateTime){
        if (is_array($hms) && count($hms)===3)
             $DT->setTime($hms[0],$hms[1],$hms[2]) ;
        return ($MySqlTime = $DT->format("Y-m-d H:i:s")) ? $MySqlTime : null ;
    }
    return null ;
}

So in your case, you use format m/d/yy : 所以在你的情况下,你使用格式m/d/yy

$sql_date = getTimeForMysql($date, "m/d/yy") ;
if ($sql_date){
  //Ok, proceed your date is correct, string is returned.
}

You don't have the century in your date, try to convert it like this: 您的日期没有世纪,请尝试将其转换为:

<?php
$date = '09/25/11';
$date = DateTime::createFromFormat('m/d/y', $date);
$date = $date->format('Y-m-d');
print $date;

Prints: 打印:

2011-09-25

Now you can insert $date into MySQL. 现在你可以在MySQL中插入$ date了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM