Jump to content

Inserting Dates


justlukeyou

Recommended Posts

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/

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;

?>
Link to comment
Share on other sites

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" );
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.