CodeIgniter Forums
Date Format by input - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: Date Format by input (/showthread.php?tid=69232)



Date Format by input - Marcolino92 - 10-23-2017

Hi, I'm using the "date" fields of a form, so of course the date will be this way: 2017-10-05

I wish the date would turn into the database in a format like this: May 24, 2017

Can someone help me? Thank you


RE: Date Format by input - PaulD - 10-23-2017

(10-23-2017, 12:28 PM)Marcolino92 Wrote: Hi, I'm using the "date" fields of a form, so of course the date will be this way: 2017-10-05

I wish the date would turn into the database in a format like this: May 24, 2017

Can someone help me? Thank you

Not sure how you get from 2017-10-5 to May 24 but assuming this is just an example.....

Convert the incoming date to a MySQL date format and store in the database. When reading the date out to a page, just format it how you want with php date functions: http://php.net/manual/en/function.date.php

In your case something like:

PHP Code:
<?php
$date 
'2017-10-05';

// convert to MySQL date
$mysql_date date('Y-m-d G:i:s'strtotime($date));

// store in database
.....

// retrieve from database
.....

// echo out format in your view
echo date('M j, Y'strtotime($mysql_date));
?>
Will output something like Oct 5, 2017

However, better to use the ordinal S
PHP Code:
echo date('M jS, Y'strtotime($mysql_date)); 
Which will output Oct 5th, 2017 or Oct 1st, 2017, whatever is appropriate to the last digit.

Hope that helps,

Paul.

PS If you really want to store the formatted date in your database as you actually asked then just use the date functions to convert it and then store it in a varchar field.
PHP Code:
$date '2017-10-05';
$date date('M j, Y'strtotime($date)); 



RE: Date Format by input - neuron - 10-23-2017

(10-23-2017, 12:28 PM)Marcolino92 Wrote: Hi, I'm using the "date" fields of a form, so of course the date will be this way: 2017-10-05

I wish the date would turn into the database in a format like this: May 24, 2017

Can someone help me? Thank you

2017-10-05  or yyyy-mm-dd format is easy to manipulate. you can order by date, filter by date in database. 
May 24, 2017 but this type of date is unusefull in database queries. you should store dates in DATETIME mysql format and reformat them after retrieving from database


RE: Date Format by input - Marcolino92 - 10-26-2017

Thanks a lot of guys, I was lost in a glass!