Getting day, month and year apart from Db - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: Getting day, month and year apart from Db (/showthread.php?tid=70221) |
Getting day, month and year apart from Db - jordyd - 03-09-2018 Dear all. I want to select the day, month and year from a SQL date field, so with the structure as 2018-03-09. To keep using that example, I want to do this: select the day (= 09), the month (= 03) and the year (=2018) apart from eachother, so that I can edit them without having to edit the entire field. I want to do that, because I want to echo the values as a value in a text form field... I hope someonecan help me with this problem. Thanks in advance. RE: Getting day, month and year apart from Db - enelson - 03-12-2018 I store dates in the db as UNIX timestamps, this way it's easy to change the value. You can then format the timestamps to a human readable date, this might help https://stackoverflow.com/a/29481073 . Use Code: <input type="date" ...> RE: Getting day, month and year apart from Db - ivantcholakov - 03-12-2018 I use a helper function of my own for this purpose. Code: function date_parts_iso($date) { Here it is a quick test: Code: list($year, $month, $day) = date_parts_iso('2018-03-09'); RE: Getting day, month and year apart from Db - ivantcholakov - 03-12-2018 If you want to use what is built in PHP, have a look at http://php.net/manual/en/function.date-parse-from-format.php An example: Code: $date_parts = date_parse_from_format('Y-m-d', '2018-03-09'); RE: Getting day, month and year apart from Db - Mr Lister - 03-18-2018 If you are obtaining your date from a database query, why not split your date with the query. MySQL has date functions for this case. Start here https://www.w3resource.com/mysql/date-and-time-functions/mysql-year-function.php E.g. SELECT YEAR( '2009-05-19' ) AS yr -> will give 'yr' as 2009. Replace the date with the appropriate field. RE: Getting day, month and year apart from Db - jordyd - 03-19-2018 I found a solution in the first message. Thank you very much for all the help! I got very nice information of you RE: Getting day, month and year apart from Db - pooja7r - 03-23-2018 The following expressions return the day for each date in the input port GET_DATE_PART ( DATE_SHIPPED, 'D' ) Get month from a date GET_DATE_PART ( DATE_SHIPPED, 'MM' ) Get year from a date GET_DATE_PART ( DATE_SHIPPED, 'Y' ) |