Forum Moderators: coopster

Message Too Old, No Replies

Date Manipulation

         

Esqulax

11:26 am on Jan 21, 2008 (gmt 0)

10+ Year Member



Hey guys,

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!

Habtom

11:28 am on Jan 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The key issue here could be the data type you set in the database. Timestamp could be very easy to manipulate or perform any of the actions you mentioned.

phranque

11:39 am on Jan 21, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



as habtom suggested i would use a canonical form, but i would suggest datetime (or just date) [dev.mysql.com] instead since timestamp has additional properties for initialization and update that you probably don't want.

Esqulax

11:46 am on Jan 21, 2008 (gmt 0)

10+ Year Member



I see what you mean.
Well... 1st things 1st...
I got this form:


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\");

Esqulax

11:46 am on Jan 21, 2008 (gmt 0)

10+ Year Member



double post..Sorry!

[edited by: Esqulax at 11:47 am (utc) on Jan. 21, 2008]

Habtom

11:50 am on Jan 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Upon submission:

$day = $_REQUEST['day'];
$month = $_REQUEST['month'];
$year = $_REQUEST['year'];

Something like the following can do it:

$fulldate = $day."-".$month."-".$year ;

phranque

12:02 pm on Jan 21, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



paraphrasing from the above linked reference:
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...

Esqulax

12:10 pm on Jan 21, 2008 (gmt 0)

10+ Year Member



Sweet,

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]

Esqulax

1:26 pm on Jan 21, 2008 (gmt 0)

10+ Year Member



Hiya, back again!

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?

Esqulax

2:53 pm on Jan 21, 2008 (gmt 0)

10+ Year Member



Ok, silly me....

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

HELLvin

2:55 pm on Jan 21, 2008 (gmt 0)

10+ Year Member



seems like ur using mktime with the wrong arguments (wrong order)
mktime ( int $hour , int $minute , int $second , int $month , int $day , int $year [, int $is_dst ] )

Esqulax

3:44 pm on Jan 21, 2008 (gmt 0)

10+ Year Member



Ok, in case anyone is following this, and is using it.. heres what i ended up doing:

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));?>