CodeIgniter Forums
Getting day, month and year apart from Db - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: Getting day, month and year apart from Db (/thread-70221.html)



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" ...>
to edit the date at the frontend. See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/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) {

    $year = 0;
    $month = 0;
    $day = 0;

    if (preg_match('/^(\d{4})-(\d{1,2})-(\d{1,2})([^\d].*)?$/', $date, $parts)) {

        if (isset($parts[1])) {
            $year = $parts[1];
        }

        if (isset($parts[2])) {
            $month = $parts[2];
        }

        if (isset($parts[3])) {
            $day = $parts[3];
        }
    }

    return array((int) $year, (int) $month, (int) $day);
}

Here it is a quick test:

Code:
list($year, $month, $day) = date_parts_iso('2018-03-09');
var_dump(compact('year', 'month', 'day'));
// array(3) { ["year"]=> int(2018) ["month"]=> int(3) ["day"]=> int(9) }



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');
var_dump($date_parts);
// array(12) { ["year"]=> int(2018) ["month"]=> int(3) ["day"]=> int(9) ["hour"]=> bool(false) ["minute"]=> bool(false) ["second"]=> bool(false) ["fraction"]=> bool(false) ["warning_count"]=> int(0) ["warnings"]=> array(0) { } ["error_count"]=> int(0) ["errors"]=> array(0) { } ["is_localtime"]=> bool(false) }



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 Smile


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' )