标签云

微信群

扫码加入我们

WeChat QR Code

I want to insert 2 date variables into startDate and endDate fields in the table.First is $today, next is $today plus $duration.I have this piece of code which used to work before, but now it does not work again.require_once('connection/config.php');$today = date("y:m:d"); //get today's date$duration = 14; //get duration$sql_insert = mysql_query("INSERT INTO adverts (cust_id, startDate, endDate)VALUES('".$today."', (CURDATE()- WeekDay(CURDATE()) +'".$duration."')");if($sql_insert){echo 'Successful';}else {echo 'Failed';}This code worked previously. But now, it's not working again.It does the insert and returns the result for the endDate as '0000-00-00'.I do not know why it is returning '0000-00-00'.Please help me.Thank you all.


Look at the documentation for MySQL Date/Time functions. There are functions for adding dates. You don't just use the + or - signs.

2019年04月22日14分55秒

(cust_id, startDate, endDate)VALUES('".$today."', (CURDATE()- WeekDay(CURDATE()) +'".$duration."'), there are 3 fields but only two values.

2019年04月22日14分55秒

I know about the 3 fields and 2 values. It was an omission. I saw the omission. But despite the omission, the curdate - weekday code ONLY works well with interval less than 14 days.

2019年04月22日14分55秒

But how will it work? Now, I have a form. The form allows a user to post advert. The user will select the duration wherein his advert will run. Now, the starting date is day the user submits the form to the table - $today, Now, I want to INSERT both $today into startDate field and add the user's duration + $today to get the endDate. From your code, how do I bring in $duration? Thank you for your contribution.

2019年04月22日14分55秒

Remember that, the user can pick any duration of ranging from 3 min to whatever number of days he chooses. Duration is in days.

2019年04月22日14分55秒

Using CURDATE() - INTERVAL WEEKDAY(CURDATE()) DAY only works when you are adding less than 14 days. From 14 days, it does not work.

2019年04月22日14分55秒

With respect, I disagree.Try SELECT CURDATE() - INTERVAL WEEKDAY(curdate()) DAY + INTERVAL 800 DAY . You'll get a perfectly valid date. TIMEDIFF() does have a maximum result of +/- 839 hours, but that's not relevant to the present problem.

2019年04月22日14分55秒