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 />';