[support] How to store/retreive date fields into/from MySql
Austin Einter
austin.einter at gmail.com
Tue Mar 29 04:49:49 UTC 2011
After all search, I found it really difficult to deal with date. I am trying
to store the date in MySql table as varchar type and placeholder as %s.
However I need two functions to convert date from array format to string
format (required while storing into table) and string format to array format
(Required while showing it in form).
The below code does that for me.
*function dateArrToStr(&$customDateArr)
{
$strDate = t($customDateArr['day']).t('-
').t($customDateArr['month']).t('-') . t($customDateArr['year']);
return $strDate;
}*
**
**
*function dateStrToArr (&$customDateStr, $length)
{
$convDate = array();
$index = 0;
$customToken = t('');
$dashPosition = 0;
for($index = 0; $index < $length; $index++)
{
if($customDateStr[$index] == t('-'))
{
if($dashPosition == 0)
{
//Set the day
$dashPosition++;
$convDate['day'] = $customToken;
}
else if ($dashPosition == 1)
{
//Set the month
$dashPosition++;
$convDate['month'] = $customToken;
}
$customToken = t('');
}
else
{
$customToken = t($customToken) . t($customDateStr[$index]) ;
}
}
//Set year
$convDate['year'] = $customToken;
return $convDate;
}*
Regards
Austin
On Mon, Mar 28, 2011 at 8:36 AM, Austin Einter <austin.einter at gmail.com>wrote:
> Hi All
> I have a date field in my form.
> How do I store/retreive the value into/from MySQL.
>
> On google search, I saw people are suggesting to use either "int" or
> "datetime" type.
> Please suggest which one needs to be used.
>
> If I use int (I hope core uses int), while storing can I just
> store "form['values']['date1']" as %d and while retreivin, what are the
> related apis to convert back to date.
>
> Regards
> Austin.
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/support/attachments/20110329/d7589d3f/attachment.html
More information about the support
mailing list