devilindisguise Posted December 14, 2014 Share Posted December 14, 2014 Hello everyone Hoping someone could lend me a hand. I have a form that takes some end-user's details and adds the date and time into a MySQL table of when the form was submitted. I wish to display that date/time + 4 days ahead using PHP. I believe the MySLQ DATE_ADD should do the trick quite nicely. In fact plumbing the following statement into phpMyAdmin gives me exactly the results I require: SELECT DATE_ADD(`datetime`,INTERVAL 4 DAY) FROM `faults` WHERE fault_id = '51'; However, just having a pig of a time getting this displayed using PHP. I'm sure this is elementary so forgive me. Here's what I had in mind but is no working. Can someone please point me in the right direction: <?php $date_query = mysqli_query($con, "SELECT DATE_ADD(`datetime`,INTERVAL 4 DAY) FROM `faults` WHERE fault_id = '51'"); while($row = mysqli_fetch_assoc($date_query)){ echo $row ['datetime']; } ?> Many thanks for your help and advice. Quote Link to comment https://forums.phpfreaks.com/topic/293094-mysql-date_add-displaying-using-php/ Share on other sites More sharing options...
mac_gyver Posted December 14, 2014 Share Posted December 14, 2014 you need to use an alias name in the query. when you ran it in phpmyadmin, the column heading/name for the value was literally - DATE_ADD(`datetime`,INTERVAL 4 DAY), which you could use in the php code (you would have to convert characters not permitted in variable names to underscores though, using print_r($row) will show what it is), but using an alias name is much easier. change your query to - SELECT DATE_ADD(`datetime`,INTERVAL 4 DAY) AS dt FROM `faults` WHERE fault_id = '51'; the php to reference the value would be - $row['dt'] Quote Link to comment https://forums.phpfreaks.com/topic/293094-mysql-date_add-displaying-using-php/#findComment-1499575 Share on other sites More sharing options...
Solution devilindisguise Posted December 14, 2014 Author Solution Share Posted December 14, 2014 Hi mac_gyver Thanks for your swift response. In typical fashion I wrote this too hastily. Thinking about it what I actually wanted to do is have it so that when the form was filled and submitted both current timestamp and the timestamp + 4 days would be inserted in the the DB. I achieved it thanks to someone who posted the following: http://coursesweb.net/forum/insert-date-after-days-added-now-function-mysql-t32.htm My code therefore now resembles: "INSERT INTO faults (datetime, target_date) VALUES (NOW(), DATE_ADD(NOW(), INTERVAL 4 DAY))"); Had to convert the MySQL columns to a TIMESTAMP format but it now does the trick. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/293094-mysql-date_add-displaying-using-php/#findComment-1499576 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.