format date mysql

76 views
Skip to first unread message

frankz

unread,
Jan 8, 2017, 2:08:50 AM1/8/17
to Fat-Free Framework
I have a field on a form with the date in the format dd / mm / yyyy (or dd-mm-yyyy),
 mysql use the yyyy-mm-dd format, how can I do the conversion?
for inserting and reading I would use the commands copyfrom and CopyTo.

Anatol

unread,
Jan 8, 2017, 4:39:47 AM1/8/17
to f3-fra...@googlegroups.com
maybe you could just update the POST date before copy (untested):

$f3->set( 'POST.yourdate', implode('-', array_reverse(explode('-', $f3->get('POST.yourdate')))) );
$f3->get('user')->copyfrom('POST');


 (if it works it would probably be better to use DATE object to convert the format)

cheers,

– anatol

Anatol

unread,
Jan 8, 2017, 5:06:11 AM1/8/17
to Fat-Free Framework
Wait!

copyfrom takes a callback function:

$db = new DB\SQL('sqlite:db/ent.sqlite');
$f3->set('user',new DB\SQL\Mapper($db,'users'));
$f3->get('user')->copyfrom('POST',function($val) {
	// the 'POST' array is passed to our callback function
    return array_intersect_key($val, array_flip(array('name','age')));
});
$f3->get('user')->save();
});


Means I would
  • sanatize your user input as the documentation suggests
  • if you cannot change the format form your datepicker (in your view) convert here the POST to be mysql conform

bcosca

unread,
Jan 8, 2017, 5:32:59 AM1/8/17
to f3-fra...@googlegroups.com
I prefer/recommend storing date/time data in a more compact integer format (as returned by time()) and perform conversion of this internal representation in the view/template where you can format the date according to the user's language preferences and/or time zone settings. This way you save bytes and make your app more flexible.

frankz

unread,
Jan 11, 2017, 3:19:54 AM1/11/17
to Fat-Free Framework
it is possible to obtain an example of use of INT as the date for insert and read from DB?
I already have the data in the DB, èpossibile a conversion?

Anatol

unread,
Jan 11, 2017, 4:16:05 AM1/11/17
to Fat-Free Framework
Hi frankz,

I think what bong means with "int" is to use a timestamp returned by time()
Further to use this as storing format in your mysql DB:

create table foo(id INT, mytimestamp INT(11));

in PHP you have date() for converting a timestamp to a date:

echo date('m/d/Y', 1299446702);

and strtotime() to convert a date to timestamp:

strtotime('11-01-2017');

Bong, speaks about a conversion in your view. This means he suggests a conversation most likely with js.
I´ve created a little example for you which should give you the idea: https://jsfiddle.net/z43t8cu3/
You would get the "unixTimestamp" from the db f.e. as json response and convert it as date 
object to your needs.

Regarding your existing dates:

I think you could do a conversion with UNIX_TIMESTAMP() as described here:

Hope this helps,

cheers,

– anatol


Reply all
Reply to author
Forward
0 new messages