close ad
WARNING: Do Not Install the DREAMWEAVER CC 2017 Update »
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

Displaying date as dd-mm-yyyy and inserting to MySQL DATETIME field?

Thread began 5/27/2014 2:34 pm by Nathon Jones Web Design | Last modified 12/29/2015 12:02 am by Ray Borduin | 9696 views | 9 replies |

Nathon Jones Web Design

Displaying date as dd-mm-yyyy and inserting to MySQL DATETIME field?

I've been using this:
<?php echo date("Y-m-d H:i:s"); ?>

...to put the date into a hidden field on my insert and update forms. How, though, can I take an existing date from a DATETIME field, display it in dd-mm-yyyy H:i format, so the UK user and understand it and easily edit it, AND ensure that when the user then tries to UPDATE that field that it will save back in the database in the correct MySQL default date format?

Thank you
NJ

Sign in to reply to this post

Jason ByrnesWebAssist

to display a recordset field on the page, you would code similar to:

<?php echo($row_recordsetName['ColumnName']); ?>

if that field is a date filed, you can form it using the date function:
<?php echo(date("m-d-Y", strtotime($row_recordsetName['ColumnName']))); ?>

Sign in to reply to this post

CraigRBeta Tester

The date is always stored in your table in the format YYYY-MM-DD.

You can manipulate how this appears on screen just by formatting it according to the php date format function

eg echo date('l F j, Y', strtotime($row_recordset['MyDate']));

http://www.w3schools.com/php/func_date_date.asp

Regardless how it is formatted, it needs to be saved back into the database table in the YYYY-MM-DD format.

To do this, I pass the date field value into a function to set the appropriate date format.

eg if I am using the datepicker tool, the date is displayed on screen as dd/mm/yyyy (We in the UK like it the proper way round)

when saved back into the database table, I pass the field value through a function

<?php
function formatUKDateMySQL($date) {
$d = str_replace(" ","",$date);
$d = explode('/', $date);
$d = $d[2].'-'.$d[1].'-'.$d[0];
return $d;
}
?>



so the dataassist insert behavior includes a line like this...

"".((isset($_POST["MyDate"]) && $_POST["MyDate"]!="" )?formatUKDateMySQL($_POST["MyDate"]):"")  .""
Sign in to reply to this post

Nathon Jones Web Design

I'm not familiar with functions, although I'm increasingly aware of how useful they are. Where does the function code sit on the page?

I've got a nice date display by using this:
<?php echo date('jS F Y h:i A', strtotime($row_rsEDITSTOCK['LEDProductUpdateDate'])); ?>

I've also got an form field (LEDProductUpdateDate), which is what saves to database, which displays the current date/time like so:
<?php echo date("Y-m-d H:i:s"); ?>

You mention passing that field value into the function. Is that done on submit of the UPDATE form?

My DataAssist Update form has this line:
$WA_fieldValuesStr = "".((isset($_POST["LEDstock"]))?$_POST["LEDstock"]:"") ."" . $WA_AB_Split . "".((isset($_POST["LEDProductUpdateDate"]))?$_POST["LEDProductUpdateDate"]:"") ."";

I'm not sure what I need to change in both the form field and UPDATE code, if you wouldn't mind helping out?
Thank you
NJ

Sign in to reply to this post

Jason ByrnesWebAssist

where the form field LEDProductUpdateDate already uses the Y-m-d format that the database requires, you do not need to use any additional formatting funcitons

<?php echo date("Y-m-d H:i:s"); ?>


formatting functions would only be \needed if the form element used a different format.

the function would need to be defined before the update record code, the used in the update record code like this:


$WA_fieldValuesStr = "".((isset($_POST["LEDstock"]))?$_POST["LEDstock"]:"") ."" . $WA_AB_Split . "".((isset($_POST["LEDProductUpdateDate"]))?formatUKDateMySQL($_POST["LEDProductUpdateDate"]):"") ."";

Sign in to reply to this post

Nathon Jones Web Design

Yes, sorry, Y-m-d is the correct format for the database but when my client goes to edit it they're bound to type in d-m-Y or, rather, dd-mm-yyyy which, on submission to database, will cause problems.

What I don't understand about the function CraigR has provided an example of is where in that function it picks up the form field, in my case "LEDProductUpdateDate"?

Should I be replacing "$date" with the name of my form field? So instead of this....

<?php
function formatUKDateMySQL($date) {
$d = str_replace(" ","",$date);
$d = explode('/', $date);
$d = $d[2].'-'.$d[1].'-'.$d[0];
return $d;
}
?>

...it becomes this:

<?php
function formatUKDateMySQL($_GET['LEDProductUpdateDate']) {
$d = str_replace(" ","",$_GET['LEDProductUpdateDate']);
$d = explode('/', $_GET['LEDProductUpdateDate']);
$d = $d[2].'-'.$d[1].'-'.$d[0];
return $d;
}
?>

Also, thinking about this, I'm concerned about what happens if my client lazily types something like 21-1-14 when entering a date.
I'd use a datepicker tool, but I want a default value of "today" in the date field.

As ever, I appreciate your advice and guidance. Thank you.
NJ

Sign in to reply to this post

CraigRBeta Tester

I would urge you to use the datepicker tool, it is easy to configure and looks nice

- also there are lots of options you can set, like limiting the date range etc, so you should be able set it to default to the current date easily enough.

http://api.jqueryui.com/datepicker/


Re the function, you don't need to change anything

all you need to do is call the function, putting in your date field as a parameter.

the function in question expects an input date in the format dd/mm/yyyy and will produce a date in the format yyyy-mm-dd

so all you need to do is change the line in the update query like so

"".((isset($_POST["LEDProductUpdateDate"]))?$_POST["LEDProductUpdateDate"]:"").""



to

"".((isset($_POST["LEDProductUpdateDate"]))?formatUKDateMySQL($_POST["LEDProductUpdateDate"]):"").""



If your date format in the field is different to dd/mm/yyyy then you will need to change the function which manipulates the string accordingly, whatever the start point is, the result should be in the format yyyy-mm-dd

Hope that helps

Sign in to reply to this post

DarylBeta Tester

Hi CraigR

How would you do this for a datetime field?

Many Thanks

Sign in to reply to this post

CraigRBeta Tester

There is a useful addon to the datepicker tool which can add the time to your selected date

http://trentrichardson.com/examples/timepicker/

You can extend the capability of the function above to reformat the time element of the field as well

Sign in to reply to this post

Puguh

<?php echo($row_recordsetName['ColumnName']); ?>

if that field is a date filed, you can form it using the date function:

php:
<?php echo(date("m-d-Y"strtotime($row_recordsetName['Column']))); ?>
Sign in to reply to this post
loading

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...