开发者

how do i get day only from javascript calender and store it into database

开发者 https://www.devze.com 2023-02-02 02:13 出处:网络
I had used javascript c开发者_JAVA百科alender in my form. User have to input a date using this calender. And date is stored in the database but what I need is that only day of a date must be saved.

I had used javascript c开发者_JAVA百科alender in my form. User have to input a date using this calender. And date is stored in the database but what I need is that only day of a date must be saved. How can I do that as I cannot make changes in javascript code as i m not good at it.

$date_customer=date("d",strtotime($_POST['datum1']));

I had also tried it by changing the column name to "tinyint" but didn't work :( .... it only stores 127 and shows 1 when record is viewed from database.


Instead of sending date to server you could send the day by using .getDay() method of javascript Date object.


I dont know the format of your date you get in your text input (when you click on one of the days in your calendar) but i'd suspect it to be dd/mm/yyyy or mm/dd/yyyy

So your php will need to be the following to only get the day

$date = explode("/",$_POST['datum1']); 
// if format is dd/mm/yyyy then use the below
$date_customer = $date[0]; 
// otherwise if format is mm/dd/yyyy then use the below  
$date_customer = $date[1];

Check out the explode function


i would save the date in MYSQL as an INT by using this function (save it as a unix timestamp) which would be helpful in comparing dates later on (up to the second) or add/remove days/years/months .

the idea would be send the whole date string generated by javascript to the PHP script "dd/mm/yyyy" ,

then in php using the explode function and create the unix timestamp using the mktime function

then save it to the database as an int ,

then when you want to read it , use the php date function to know the day/month/year/hour/second/minute , you could then also add hours (+3600) or days (+3600*days) etc... , or even get range of dates and many other functionalities you may use later ...

cheers


I suppose that you use mysql (TINYINT are mysql specific).

TINYINT are integer and in php integer are usually not prefixed by zeros

Use a DATE field and format it when you report it. You can use mysql date_format(date,"%d") function in your query. Or the php date function.

select date_format(date_field,"%d") from some_table;

You can use a VARCHAR(2) to store the date (ugly).

If you stick to store only the DAY in an int or tynint. use sprintf("%02d",$day) to format it correctly in php.

0

精彩评论

暂无评论...
验证码 换一张
取 消