Jump to content

MySQL DATE_ADD - Displaying using PHP


devilindisguise
Go to solution Solved by devilindisguise,

Recommended Posts

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.

 

Link to comment
Share on other sites

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']

Link to comment
Share on other sites

  • Solution

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.