Forum Moderators: coopster
Im making a database that need to deal with dates, and im having trouble getting my head round figuring out how to do it..
may be a long one though...
Ok, so i want the user to be able to enter the date, in a select box style (3 boxes, D,M,Y),
Then save that date to the MySQL database.
I also need to to be able to add x amount of days to that date (again user defined)
and stick THAT date in the database aswell.
im using strtotime() for a different element of the site, where the date is incremented, and have a feeling thats the function i need to be using.
Any pointage in the right direction would be really appreciated!
Date:<input type="text" name="day" size="2" value="<?php echo date("d");?>">/
<input type="text" name="month" size="2" value="<?php echo date("m");?>">/
<input type="text" name="year" size="2" value="<?php echo date("y");?>">
Which can take user imputted values (Im intending to pop an error correction thingy on it, so's the user cant enter the 42nd of the 84th)
how can i concatenate (yeah, big word :p ) "day", "month", and "year" in such a way, sos i can just save it?
would it be like...
(pseudocode)
$fulldate = $day + $month + $year
then
insert into table (userdate) values (\"$fulldate\");
You can specify DATETIME, DATE, and TIMESTAMP values using any of a common set of formats:
- As a string in either 'YYYY-MM-DD' or 'YY-MM-DD' format. A “relaxed” syntax is allowed here, too. For example, '98-12-31', '98.12.31', '98/12/31', and '98@12@31' are equivalent.
- As a string with no delimiters in either 'YYYYMMDD' or 'YYMMDD' format, provided that the string makes sense as a date. For example, '19970523' and '970523' are interpreted as '1997-05-23', but '971332' is illegal (it has nonsensical month and day parts) and becomes '0000-00-00'.
therefore i would suggest reversing the concatenation order...
although, i set my SQL table type to DATE, and, showed a lil mod i needed to make:
$fulldate = $year."-".$month."-".$day ;
Cos the database stores it as YYYY-MM-DD
Right.. adding said amount of days...
$date_2 = mktime(0,0,0,0,date("d")+."$extra_days",0);
then add date_2 to my oh so lovely database...does that look right?
::EDIT::
got there while i was typing phranque :p
[edited by: Esqulax at 12:11 pm (utc) on Jan. 21, 2008]
im getting some odd results, and im not sure why...
I have my form as above for getting the dates from the user, the variables being called
&day, $month,$year.
i then have
$arr_timestamp = mktime(0,0,0,$year,$month,$day);
So it'll be easier to strtime it and add the user-defined days.
when testing i did
echo(' '.$arr_timestamp);
and get1627776000 as the result.
any Ideas?
$arr_timestamp = date('d-m-y',mktime(0,0,0,$month,$day,$year));
allows the users inputted to be made to a unix timestamp
$dep_timestamp = date('d-m-y', strtotime("+{$extra_days} day"));
Makes a timestamp +1 day. BUT thats for TODAYS timestamp, now im trying to work out how to minus 1 day from arr_timestamp
[edited by: Esqulax at 2:54 pm (utc) on Jan. 21, 2008]
I wanted to have the user enter a date and a number of days. i then wanted to add the number of days to the date entered.
My basis is "booking a hotel room"
$arrival is the arrival date, the user puts in. this is in a nice and view friendly format.
$formatstamp re-shuffles it a bit to make it "unix timestamp happy"
this works, but if theres a better way of doing this, feel free to let me know!
<!-- Date and legnth of stay-->
Date:
<input type="text" name="day" size="2" value="<?php echo date("d");?>">/
<input type="text" name="month" size="2" value="<?php echo date("m");?>">/
<input type="text" name="year" size="2" value="<?php echo date("y");?>">
<?php
//concatanate the inputted values, and format
$fulldate= $year."-".$month."-".$day ;
$arrival = date('d-m-y',mktime(0,0,0,$month,$day,$year));//format the arrival dates to a timestamp
$formatstamp = date('y-m-d',mktime(0,0,0,$month,$day,$year));
$arr_timestamp = strtotime($formatstamp);
?>
<br><br>
Number of nights: <input type="text" name="nights" size="2" value="1">
<?php
//adding nights staying to the timestamp
$dep_timestamp = date('d-m-y', strtotime("+{$nights} day", $arr_timestamp));?>