justlukeyou Posted April 12, 2013 Share Posted April 12, 2013 I am using the jQuery code to insert a US format date format into a table. However I have two frustrating problems. Firstly I can only insert the value as text so I cant use it to calcualte with. I have tried the five date and time values in MyAdmin but each time I run the jQuery code it inserts the value as zeroes. $startdate = $row['eventstart']; $enddate = $row['eventend']; $numberofdays = $startdate - $enddate; I also found this but it states that the date format is english but the jQuery datepicker is in an American format. Any suggestions please on what I need to insert a date format instead of text. From there I should be able to calculate the number of days. http://php.net/manual/en/function.strtotime.php echo $numberofdays; $days = (strtotime($$startdate) - strtotime($enddate)) / (60 * 60 * 24); print $days; http://jqueryui.com/datepicker/ Quote Link to comment https://forums.phpfreaks.com/topic/276880-inserting-dates/ Share on other sites More sharing options...
Barand Posted April 12, 2013 Share Posted April 12, 2013 When storing dates in a database always use yyyy-mm-dd format (type DATE, or if times are required as well, DATETIME or TIMESTAMP). In this format dates can be compared and sorted and you can use the dozens of datetime function the MySQL provides. Any other format is as much use as a chocolate teapot. Store dates for functionality, not prettiness. Quote Link to comment https://forums.phpfreaks.com/topic/276880-inserting-dates/#findComment-1424443 Share on other sites More sharing options...
justlukeyou Posted April 13, 2013 Author Share Posted April 13, 2013 Thanks, However the jQuery does insert the date into the table when I set the column value as DATE. When I set the cell as TEXT it will insert the date, but not when I select DATE. When I insert the value when the table is set at DATE it only inserts 0000-00-00 into the table. Quote Link to comment https://forums.phpfreaks.com/topic/276880-inserting-dates/#findComment-1424543 Share on other sites More sharing options...
justlukeyou Posted April 13, 2013 Author Share Posted April 13, 2013 If I manually enter the dates into DATE I can get the calculation to work which I am really please about. However I cant get the jQuery to insert a value into DATE. <?php $startdate = $row['eventstart']; $enddate = $row['eventend']; $days = (strtotime($enddate) - strtotime($startdate)) / (60 * 60 * 24); print $days; ?> Quote Link to comment https://forums.phpfreaks.com/topic/276880-inserting-dates/#findComment-1424544 Share on other sites More sharing options...
jcbones Posted April 13, 2013 Share Posted April 13, 2013 You need to set the date format in jQuery's datepicker. dateFormatType: String Default: "mm/dd/yy" The format for parsed and displayed dates. For a full list of the possible formats see the formatDate function. Code examples: Initialize the datepicker with the dateFormat option specified: 1 $( ".selector" ).datepicker({ dateFormat: "yy-mm-dd" }); Get or set the dateFormat option, after initialization: 1 2 3 4 5 // getter var dateFormat = $( ".selector" ).datepicker( "option", "dateFormat" ); // setter $( ".selector" ).datepicker( "option", "dateFormat", "yy-mm-dd" ); Quote Link to comment https://forums.phpfreaks.com/topic/276880-inserting-dates/#findComment-1424571 Share on other sites More sharing options...
justlukeyou Posted April 13, 2013 Author Share Posted April 13, 2013 Thanks, That was easier than I thought it would be. Quote Link to comment https://forums.phpfreaks.com/topic/276880-inserting-dates/#findComment-1424605 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.