CodeIgniter Forums
CURDATE() and INTERVAL in codeigniter - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: CURDATE() and INTERVAL in codeigniter (/showthread.php?tid=49193)

Pages: 1 2 3 4


CURDATE() and INTERVAL in codeigniter - El Forum - 02-15-2012

[eluser]Thiago Leao[/eluser]
[quote author="InsiteFX" date="1329310285"]You can try this, but then you will need to use set on all the fields.
Code:
$this->db->set('date_finish = DATE_ADD(CURDATE(), INTERVAL 30 DAY)');

// or
$array = array('id_products' => $id_products, 'date_start' => CURDATE(), 'date_finish' => 'DATE_ADD(CURDATE() + INTERVAL 30 DAYS)');
[/quote]

did not work!

DB

id_buy id_products id_user date_start date_finish
12 178 0 0000-00-00 0000-00-00


CURDATE() and INTERVAL in codeigniter - El Forum - 02-15-2012

[eluser]Mauricio de Abreu Antunes[/eluser]
Are you using MySQL?
You need to pass date with the right format.

Print your curdate and compare with your format database date field.

When you insert a strange string into date colummn, he just inserts 0000-00-00.

[EDIT]: why are you using CURTADE as php function? Put this function into your string.


CURDATE() and INTERVAL in codeigniter - El Forum - 02-15-2012

[eluser]Thiago Leao[/eluser]
using the same string is returned as zero!


CURDATE() and INTERVAL in codeigniter - El Forum - 02-15-2012

[eluser]Mauricio de Abreu Antunes[/eluser]
Code:
//create your array first
$array = array();
//use the set method
$this->db->set($array);
$this->db->insert('your_table_name');

according to manual, its not needed to pass your array in insert, just your table name.


CURDATE() and INTERVAL in codeigniter - El Forum - 02-15-2012

[eluser]Thiago Leao[/eluser]
???


CURDATE() and INTERVAL in codeigniter - El Forum - 02-15-2012

[eluser]Mauricio de Abreu Antunes[/eluser]
Don't argue with "???". Its a little bit offensive and not mature for a programmer.

Try out this:
Code:
function insertBuy($id_products) {
  $this->load->helper('date');
  $array = array('id_products' => $id_products,
                 'date_start' => 'CURDATE()',
                 'date_finish'=> 'CURDATE() + INTERVAL 30 DAYS');
  $this->db->set($array);
  $this->db->insert('buy');
}



CURDATE() and INTERVAL in codeigniter - El Forum - 02-15-2012

[eluser]InsiteFX[/eluser]
What type od date field our you using in your table?

I think he should be using NOW() not CURDATE() by the looks of the date he displayed above!




CURDATE() and INTERVAL in codeigniter - El Forum - 02-15-2012

[eluser]Mauricio de Abreu Antunes[/eluser]
CURDATE() in MySQL returns this format YYYY-MM-DD and NOW() is YYYY-MM-DD HH:MI:SS

You use CURTADE for insert data in DATE type
And NOW for insert data in DATETIME type

Code:
SELECT CURDATE(); //2012-02-15
SELECT NOW();     //2012-02-15 12:21:00



CURDATE() and INTERVAL in codeigniter - El Forum - 02-15-2012

[eluser]Mauricio de Abreu Antunes[/eluser]
User your add days like this:
Code:
SELECT DATE_ADD('2012-02-15', INTERVAL 1 DAY); //2012-02-16




CURDATE() and INTERVAL in codeigniter - El Forum - 02-15-2012

[eluser]Thiago Leao[/eluser]
when I insert manually in mysql, it works perfectly!!!!
It is giving some problem in converting to CI!

Code:
INSERT INTO buy (id_products, date_start, date_finish) VALUES ('1', CURDATE(), CURDATE() + INTERVAL 30 DAY)