Welcome Guest, Not a member yet? Register   Sign In
Date Helper
#1

HI im italian , my date format is 'd-m-Y'.

For example i want insert 03-12-2017.

In db i have a date field ('data_fine')  , when i save the date in db  i do this :

nice_date($this->input->post('data_fine'),'Y-m-d');

But ther'is a problem in db is inserted 2017-03-12.

If i want insert a value with day >12 in db is inserted 01-01-1970.

How can i use this helper properly ?
Reply
#2

(This post was last modified: 11-25-2017, 08:02 PM by ivantcholakov. Edit Reason: a typo )

date_helper.php was created a long time ago, even before PHP 5.2, if I remember correctly.
The contemporary PHP versions provide features that would help better:

See this page: http://php.net/manual/en/class.datetime.php
and especially this: http://php.net/manual/en/datetime.createfromformat.php

The intl extension also provides date format conversions http://php.net/manual/en/class.intldateformatter.php
but it can be disabled for installation on the server.

These classes are usually verbose when they are used directly.
For your convenience you can make your own conversion helper
functions for both directions, like these ones:

Code:
// These are two custom date format conversion helper functions
// with long ugly names. You might choose other convenient names.

if (!function_exists('date_local_format_from_iso')) {

    function date_local_format_from_iso($date = null) {

        if ($date === NULL || is_string($date) && $date === '') {
            return NULL;
        }

        if (($date_time_obj = date_create($date)) !== FALSE) {
            return $date_time_obj->format('d-m-Y');
        }

        return NULL;
    }
}

if (!function_exists('date_local_format_to_iso')) {

    function date_local_format_to_iso($date = null) {

        $date = (string) $date;

        if ($date == '') {
            return NULL;
        }

        $date = substr($date, 0, 10);

        if (($date_time_obj = date_create_from_format('d-m-Y', $date)) !== FALSE) {
            return $date_time_obj->format('Y-m-d');
        }

        return NULL;
    }
}

Usage examples:

Code:
// Quick testing:

//--------------------------------------------------------

echo 'Date format coneversion from ISO to Italian format:';
echo '<br />';

echo date_local_format_from_iso('2017-11-26 03:56:12');
// Expected result: '26-11-2017'
echo '<br />';

echo date_local_format_from_iso('2017-11-26 03:56');
// Expected result: '26-11-2017'
echo '<br />';

echo date_local_format_from_iso('2017-11-26T03:56:12');
// Expected result: '26-11-2017'
echo '<br />';

echo date_local_format_from_iso('2017-11-26T03:56');
// Expected result: '26-11-2017'
echo '<br />';

echo date_local_format_from_iso('2017-11-26');
// Expected result: '26-11-2017'
echo '<br />';

echo date_local_format_from_iso('2018-2-1');
// Expected result: '01-02-2018'
echo '<br />';

echo date_local_format_from_iso('GARBAGE');
// Expected result: NULL
echo '<br />';

echo date_local_format_from_iso('');
// Expected result: NULL
echo '<br />';

echo date_local_format_from_iso(null);
// Expected result: NULL
echo '<br />';

echo date_local_format_from_iso('now');
// Expected result (a side feature): '26-11-2017' (the current date)
echo '<br />';

echo 'END';
echo '<br />';

echo '--------------------------------------------------------------------------';
echo '<br />';

echo 'Date format coneversion from Italian to ISO format:';
echo '<br />';

echo date_local_format_to_iso('26-11-2017 03:56:12');
// Expected result: '26-11-2017'
echo '<br />';

echo date_local_format_to_iso('26-11-2017 03:56');
// Expected result: '26-11-2017'
echo '<br />';

echo date_local_format_to_iso('26-11-2017');
// Expected result: '26-11-2017'
echo '<br />';

echo date_local_format_to_iso('1-2-2018');
// Expected result: '2018-02-01'
echo '<br />';

echo date_local_format_to_iso('GARBAGE');
// Expected result: NULL
echo '<br />';

echo date_local_format_to_iso('');
// Expected result: NULL
echo '<br />';

echo date_local_format_to_iso(null);
// Expected result: NULL
echo '<br />';

echo 'END';
echo '<br />';
Reply
#3

It's a date helper, not a database helper ...
Reply




Theme © iAndrew 2016 - Forum software by © MyBB