• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to insert a date into the database?

i want to insert time and date along with post details ? how insert date and time in my database

First, You posted in the job board. Post in the right section next time ;-)

Onto your question...
You could use the date() function:

echo date('m-d-Y');  // outputs 09-19-2009

so on your insert statement, it would look something like this:

$date = date('m-d-Y');
$this->db->insert('mytable', $date);

If you want more control, down to the second, you could use Epoch time. Look on the php.net manual for the time() function.

Stick with ISO 8601 for your date formats - apart from being implicitly sensible, it aligns (more closely) with MySQL's native format.

You may be better off generating your datestamps from within your database directly, using the NOW() function (in MySQL - there's probably comparable functions for other RDBMS's).

Mind, if you're asking this question, then you probably don't want to track timezones for different users, etc, so it's probably less important for you to have accurate date information.

I like to store date/times as an integer, and stick the seconds into that field, allows you to do anything you like when you pull it out then, display it however you like, change it around to and all.

That's going to be nice and fast if you're just doing comparisons between two dates, but I think you get some measurable performance benefits once you start using the native date and time types - for example selecting a bunch of records from a given month. Sure, you can work out your boundaries for each month, but then you end up with a bunch of new functions, and a bit of overhead.

There are CI/php functions to convert 8601 / MySQL date formats back into unix time (number of seconds since 19700101T0000UTC Wink

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.