Storing Date in MySQL database? Out of ideas...

Von Steiner 01 Apr, 2011

I'm out of ideas on how to get a date into my MySQL database correctly. I have a form working OK with all the other fields and have done a lot of the form coding by hand after starting with the wizard. My form has two date fields, each in the UK/AU format dd/mm/yyyy.

I understand that MySQL stores dates in its own format 'Y-m-d H:i:s' and I have tried to wrestle the date data into shape with some PHP I gathered from a post in this forum:

JRequest::setVar('empstartdate') = date('Y-m-d H:i:s', strtotime(JRequest::getVar('empstartdate')));
JRequest::setVar('empenddate') = date('Y-m-d H:i:s', strtotime(JRequest::getVar('empenddate'))); ?>

'empstartdate' and 'empenddate' are text fields in the form and I put this code into the onSubmit boxes in the Form Code tab. This gives me the error:

Fatal error: Can't use function return value in write context in C:\xampp\Joomla1-5\components\com_chronocontact\libraries\customcode.php(64) : eval()'d code on line 3

I have the date picker working and I've tried Y-m-d, but I can't get past the validation, despite the fact that it's turned off (Enable validation is set to No).

The other fields all link up fine (I can see the data in the MySQL records using PHPMyAdmin), but the date fields just show 0000-00-00.

What am I missing here? I've read through my ecopy of Bob's book and all the articles I can find, as well as the forum here. It's probably in front of my face, but I can't see it.

I can think of kludges to try, but would really like to know how to get any user-friendly date format into the DB.

I'm using Chronoforms 3.2 on Joomla 1.5.22.

Any suggestions greatly appreciated.

Thanks very much in advance.

rushinge 01 Apr, 2011

Fatal error: Can't use function return value in write context in C:\xampp\Joomla1-5\components\com_chronocontact\libraries\customcode.php(64) : eval()'d code on line 3

Error means that you can't use a function's return value without saving it to a variable first.

Changing your code to

$empstartdate = JRequest::getVar('empstartdate');
$empenddate = JRequest::getVar('empenddate');

JRequest::setVar('empstartdate') = date('Y-m-d H:i:s', strtotime($empstartdate));
JRequest::setVar('empenddate') = date('Y-m-d H:i:s', strtotime($empenddate));

may fix it.


Just realized you're using setVar incorrectly. That's probably why you're getting the error in the first place. You're trying to assign a value to the result of JRequest::setVar() which is a function, not a variable, so you can't assign a value to it.

$empstartdate = JRequest::getVar('empstartdate');
$empenddate = JRequest::getVar('empenddate');

$eStartDate = date('Y-m-d H:i:s', strtotime($empstartdate);
$eEndDate = date('Y-m-d H:i:s', strtotime($empenddate);

JRequest::setVar('empstartdate', $eStartdate);
JRequest::setVar('empenddate', $eEndDate);
GreyHead 01 Apr, 2011
Hi Von Steiner ,

There's another problem with strtotime - it assumes as US date format so will treat today's date 01/04/2100 as Jan 4th.

This is a little longer but should remove the ambiguity:
$dates = array('empstartdate', 'empenddate');
foreach ( $dates as $d ) {
  $temp = JRequest::getString($d, '00/00/0000', 'post');
  $temp = explode('/', $temp);
  $temp = array_reverse($temp);
  $temp = implode('-', $temp);
  JRequest::setVar($d, $temp);

Von Steiner 02 Apr, 2011
Hi Bob,

Thanks very much, that did the trick. I guess the form writes to the database before the email goes out? It only worked when I put the code in the 'before email' box.

You guys give unbelievably great support! I am very impressed - and grateful.


GreyHead 02 Apr, 2011
Hi Justin,

Normally the the sequence is:

OnSubmit Before Email > Email > Plug-ins > OnSubmit After Email > DB Connection

You can change the order with the RunOrder tab and the Saving Data/Email order option on the DB Connection tab.

The OnSubmit
This topic is locked and no more replies can be posted.