How to save date into custom table

For Joomla! 3.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Locked
arek1
Joomla! Apprentice
Joomla! Apprentice
Posts: 24
Joined: Sun Jul 29, 2007 6:06 pm

How to save date into custom table

Post by arek1 » Fri Sep 21, 2018 5:16 pm

Hi,

I struggling with saving date into custom Joomla table. This code creating error for me:

Code: Select all

$db = JFactory::getDbo();
$date = new JDate('now');
$query = $db->getQuery(true);
$columns = array('user_id', 'last_czytanie');
$values = array('15', $date); //this line create error
			
$query
    ->insert($db->quoteName('custom_czytane'))
    ->columns($db->quoteName($columns))
    ->values(implode(',', $values));
			
$db->setQuery($query);
$db->execute();
I am marked with comment line which in my opinion creating error. I suppose it is something related to date format.

I was trying:

Code: Select all

$values = array('15', '2018-01-01');
and this:

Code: Select all

$values = array('15', '2018-01-01 01:01:01');
and this does not work too.

What I am doing wrong?

Best regards
Arek

reviewermo
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Tue Sep 11, 2018 12:41 pm

Re: How to save date into custom table

Post by reviewermo » Sat Sep 22, 2018 3:01 am

You may try this code

Code: Select all

$values = array('15', 'Y/m/d');

SharkyKZ
Joomla! Hero
Joomla! Hero
Posts: 2903
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: How to save date into custom table

Post by SharkyKZ » Sat Sep 22, 2018 8:56 am

Use $db->quote() to escape strings. For date you should also use $date->toSql() to make sure it's formatted correctly. So change the erroneous line to this:

Code: Select all

$values = array(15, $db->quote($date->toSql()));

arek1
Joomla! Apprentice
Joomla! Apprentice
Posts: 24
Joined: Sun Jul 29, 2007 6:06 pm

Re: How to save date into custom table

Post by arek1 » Sun Sep 30, 2018 3:29 pm

It is working, Thank you.


Locked

Return to “Joomla! 3.x Coding”