Forums

Posting date to mysql

cbehan 12 Dec, 2008
What's the trick? I need to record the contents of a datetimepicker field to my mysql database in a date field...and there are vague references to it here but nothing spells it out.

Can someone help me out please?
Max_admin 12 Dec, 2008
Hi cbehan,

the date format coming out from the picker is not in the mysql format i think so use a VARCHAR field instead!

regards
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
cbehan 12 Dec, 2008
Is there a way to convert it? I need to use the data for some reporting and dates would make it much easier.
Max_admin 12 Dec, 2008
I think there is a trick to use, but first try to have a DB field of type VARCHAR, do you get your data saved there ?
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
cbehan 12 Dec, 2008
If it's a varchar it saves the date to the db in dd/mm/yyyy format.
GreyHead 12 Dec, 2008
Hi cbehan,

Here are a couple of snippets that I've used to convert date for a calendar to a MySQL date and back. My calendar format is 'dd-mm-yyyy' so you'll need to modify them a bit. To access from the database
$some_date = date('d-m-Y', strtotime($some_date));
 if ( $some_date == '01-01-1970' ) {
 $some_date = '';
}
and to save to the database
if ( $some_date == '' ) {
  $some_date = '0000-00-00 00:00';
} else {
  $temp_date = explode('-', $some_date);
  $some_date = $temp_date[2]."-".$temp_date[1]."-".$temp_date[0]." 00:00:00";
}

Bob
cbehan 12 Dec, 2008

Hi cbehan,

Here are a couple of snippets that I've used to convert date for a calendar to a MySQL date and back. My calendar format is 'dd-mm-yyyy' so you'll need to modify them a bit. To access from the database

$some_date = date('d-m-Y', strtotime($some_date));
 if ( $some_date == '01-01-1970' ) {
 $some_date = '';
}
and to save to the database
if ( $some_date == '' ) {
  $some_date = '0000-00-00 00:00';
} else {
  $temp_date = explode('-', $some_date);
  $some_date = $temp_date[2]."-".$temp_date[1]."-".$temp_date[0]." 00:00:00";
}

Bob




Thanks Bob, but one more question: Where do I put this code?
Max_admin 14 Dec, 2008
Hi cbehan,

try this in the onsubmit before email box (assuming you have some email setup in your form ?)


<?php
JRequest::setVar('date_field_name', date('d-m-Y', strtotime(JRequest::getVar('date_field_name'))));
?>
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
dion 08 Mar, 2009
Where should i put this code?

<?php
JRequest::setVar('date_field_name', date('d-m-Y', strtotime(JRequest::getVar('date_field_name'))));
?>

Here ?
Max_admin 08 Mar, 2009
right, I see you changed it alittle but I'm not sure if it will work after your changes!

Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
dion 08 Mar, 2009
is not working in my case... 😟
But i solved my case using ServerSide Validation. 😀 I put my code there.

<?php
$dt=explode("/",JRequest::getVar('date_1'));
$dt_time=$dt[2]."-".$dt[1]."-".$dt[0]." - 00:00:00";
JRequest::setVar( 'date_1', $dt_time);
?>

note: i have to use explode because
date('Y-m-d - H:i:ss',strtotime(JRequest::getVar('date_1')))
didn't work. very strange !! it always return "01/01/1970 07:00:00". did i miss something?
Max_admin 09 Mar, 2009
I think the strtotime function didn't like something with the string!! :?
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
This topic is locked and no more replies can be posted.