Welcome Guest, Not a member yet? Register   Sign In
Getting day, month and year apart from Db
#1

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.
Reply
#2

(This post was last modified: 03-12-2018, 12:31 PM by enelson.)

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...input/date
Reply
#3

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) }
Reply
#4

(This post was last modified: 03-12-2018, 01:21 PM by ivantcholakov. Edit Reason: single quotes )

If you want to use what is built in PHP, have a look at http://php.net/manual/en/function.date-p...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) }
Reply
#5

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-an...nction.php

E.g. SELECT YEAR( '2009-05-19' ) AS yr -> will give 'yr' as 2009. Replace the date with the appropriate field.
Reply
#6

I found a solution in the first message. Thank you very much for all the help!
I got very nice information of you Smile
Reply
#7

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




Theme © iAndrew 2016 - Forum software by © MyBB