简体   繁体   中英

error in save date in database with php

want to be able to store the current date from PHP in to a mysql table.

$date = sprintf("%'04s-%'02s-%'02s",$year_number,$month_number,$day_number);

$sql="INSERT INTO  `prg omran`.`paid`(`Comapny_id`,`Amount`,`Date`,`Creditrecord`) VALUES ('".$ID."','".$credit."','".$date."','".$yes."')";

Date's column type in database is DATE.

but date save in database like this: 0000-00-00

help me.

If you are getting the current date, just use one of the built-in MySQL functions for that:

$sql="INSERT INTO  `prg omran`.`paid`(`Comapny_id`,`Amount`,`Date`,`Creditrecord`) 
VALUES ('".$ID."','".$credit."',curdate(),'".$yes."')";

You won't ever have to worry about formatting or any sort of funny inputs and you can still do all sorts of addition, subtraction and the like using one of the many other functions.

Edit: I have just looked up some information on the date system and you will run into problems trying to squeeze that data into a date field. The first six months have 31 days, the next 5 have 30 days, then the last has 29 - unless it is a leap year in which case it has 30. The date field simply won't LET you insert these values into a date field.

You might need to convert your data to a timestamp and write a function to encode/decode it into the format you need.

try this

$date = sprintf("%'04s-%'02s-%'02s",$year_number,$month_number,$day_number);

$sql="INSERT INTO  `prg omran`.`paid`(`Comapny_id`,`Amount`,`Date`,`Creditrecord`) VALUES      ('".$ID."','".$credit."',".$date.",'".$yes."')";

or

if you want to inset today's date use

$sql="INSERT INTO  `prg omran`.`paid`(`Comapny_id`,`Amount`,`Date`,`Creditrecord`) 
VALUES ('".$ID."','".$credit."',curdate(),'".$yes."')";

我需要在数据库中存储Date字段,例如VARCHAR(10)因为,mysql服务器中的DATE类型不支持波斯语(波斯语)日期。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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