I have a submit form with a text field. I want it to auto fill the date "7 days" from now into MySQL, and also be editable.
The code below worked fine a few days ago, well until a new month is coming around the corner.

$SevenDay = date("d") + 7;
<?php echo $year,"-",$month,"-",$SevenDay ?>

Is there a simple way to do this?

Recommended Answers

All 3 Replies

the php string to time function strtotime() allows this

<?php echo date('l dS \o\f F Y h:i:s A', strtotime('+7 day')); ?>

format the date() how you need,

storing the timestamp in the db is more efficient than storing any text representation of the date and time
comparisons are simpler, date ranges are simpler, each record is only 10bytes
it is easier to process a numeric timestamp and echo the formatted version on output

Thanks for replying almostbob.
I just got it to work with this, when I received the email.

<?php
$SevenDay = mktime(0,0,0,date("m"),date("d")+7,date("Y"));
echo date("Y-m-d", $SevenDay);
?>

I will look into your suggestion, as I have heard that before.
Thanks again

four functions in two lines of code, or two functions in one line of code
extrapolated over a very large dataset, one times out, one don't, because whatever you do on input, used the same way on report generation or data manipulation slows down
once the dates are stored in the database as text, every manipulation will have to take every date and convert it to a timestamp to compare it, to select on date ranges
extrapolated over a very large database, timeout

select * where date >  1251245826  and date < 126235900

is quite fast compared to
?? -off the top of my head its been so long since I used anything but a timestamp that the code to
convert each date in the database to a timestamp,
store the id and timestamp in a temp table,
select id on the basis of the timestamp value in the temp table,
select * where id is in the temp output dataset,
drop the temp table
just wont pop into my head
but I guess that it will take much longer than one line of code to execute

database design is probably the most important thing to consider at this point
a few records, anything will work (not work well, just work)
increasing record numbers requires good design, by which time it is too late to implement design changes

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.