diff --git a/doc/Developing/Style-Guidelines.md b/doc/Developing/Style-Guidelines.md
index cef63a7ee..9fc9db3b9 100644
--- a/doc/Developing/Style-Guidelines.md
+++ b/doc/Developing/Style-Guidelines.md
@@ -37,4 +37,24 @@ do this but provides so much flexibility along with stopping the need for retrie
place.
As an example pull request, see [PR 706](https://github.com/librenms/librenms/pull/706/files) to get an idea of what
-it's like to convert an existing pure html table to Bootgrid.
\ No newline at end of file
+it's like to convert an existing pure html table to Bootgrid.
+
+### Datetime format
+
+When displaying datetimes, please ensure you use the format YYYY-MM-DD mm:hh:ss where possible, you shouldn't change the
+order of this as it will be confusing to users. Cutting it short to just display YYYY-MM-DD mm:hh is fine :).
+
+To keep things consistent we have the following variables which should be used rather than to format dates yourself.
+This has the added benefit that users can customise the format:
+
+```php
+# Date format for PHP date()s
+$config['dateformat']['long'] = "r"; # RFC2822 style
+$config['dateformat']['compact'] = "Y-m-d H:i:s";
+$config['dateformat']['time'] = "H:i:s";
+
+# Date format for MySQL DATE_FORMAT
+$config['dateformat']['mysql']['compact'] = "%Y-%m-%d %H:%i:%s";
+$config['dateformat']['mysql']['date'] = "%Y-%m-%d";
+$config['dateformat']['mysql']['time'] = "%H:%i:%s";
+```
diff --git a/html/includes/reports/alert-log.pdf.inc.php b/html/includes/reports/alert-log.pdf.inc.php
index b44023620..5ee0e91ea 100644
--- a/html/includes/reports/alert-log.pdf.inc.php
+++ b/html/includes/reports/alert-log.pdf.inc.php
@@ -32,7 +32,7 @@ $pdf->AddPage('L');
$numresults = 250;
}
- $full_query = "SELECT D.device_id,name,state,time_logged,DATE_FORMAT(time_logged, '%D %b %Y %T') as humandate $query LIMIT $start,$numresults";
+ $full_query = "SELECT D.device_id,name,state,time_logged,DATE_FORMAT(time_logged, '".$config['dateformat']['mysql']['compact']."') as humandate $query LIMIT $start,$numresults";
foreach (dbFetchRows($full_query, $param) as $alert_entry) {
$hostname = gethostbyid(mres($alert_entry['device_id']));
diff --git a/html/includes/table/alert-schedule.inc.php b/html/includes/table/alert-schedule.inc.php
index ba6f9884c..635e67be9 100644
--- a/html/includes/table/alert-schedule.inc.php
+++ b/html/includes/table/alert-schedule.inc.php
@@ -46,7 +46,7 @@ if ($rowCount != -1) {
$sql .= " LIMIT $limit_low,$limit_high";
}
-$sql = "SELECT `S`.`schedule_id`, DATE_FORMAT(`S`.`start`, '%D %b %Y %T') AS `start`, DATE_FORMAT(`S`.`end`, '%D %b %Y %T') AS `end`, `S`.`title` $sql";
+$sql = "SELECT `S`.`schedule_id`, DATE_FORMAT(`S`.`start`, '".$config['dateformat']['mysql']['compact']."') AS `start`, DATE_FORMAT(`S`.`end`, '".$config['dateformat']['mysql']['compact']."') AS `end`, `S`.`title` $sql";
foreach (dbFetchRows($sql,$param) as $schedule) {
$status = 0;
diff --git a/html/includes/table/alertlog.inc.php b/html/includes/table/alertlog.inc.php
index 860d6066f..0c8e515e6 100644
--- a/html/includes/table/alertlog.inc.php
+++ b/html/includes/table/alertlog.inc.php
@@ -39,7 +39,7 @@ if ($rowCount != -1) {
$sql .= " LIMIT $limit_low,$limit_high";
}
-$sql = "SELECT D.device_id,name AS alert,state,time_logged,DATE_FORMAT(time_logged, '%D %b %Y %T') as humandate,details $sql";
+$sql = "SELECT D.device_id,name AS alert,state,time_logged,DATE_FORMAT(time_logged, '".$config['dateformat']['mysql']['compact']."') as humandate,details $sql";
$rulei = 0;
foreach (dbFetchRows($sql,$param) as $alertlog) {
diff --git a/html/includes/table/eventlog.inc.php b/html/includes/table/eventlog.inc.php
index c2bd64e4f..d5e5e7d60 100644
--- a/html/includes/table/eventlog.inc.php
+++ b/html/includes/table/eventlog.inc.php
@@ -46,7 +46,7 @@ if ($rowCount != -1) {
$sql .= " LIMIT $limit_low,$limit_high";
}
-$sql = "SELECT `E`.*,DATE_FORMAT(datetime, '%D %b %Y %T') as humandate $sql";
+$sql = "SELECT `E`.*,DATE_FORMAT(datetime, '".$config['dateformat']['mysql']['compact']."') as humandate $sql";
foreach (dbFetchRows($sql,$param) as $eventlog) {
$dev = device_by_id_cache($eventlog['host']);
diff --git a/html/includes/table/syslog.inc.php b/html/includes/table/syslog.inc.php
index ca041fe2a..81df05d28 100644
--- a/html/includes/table/syslog.inc.php
+++ b/html/includes/table/syslog.inc.php
@@ -60,7 +60,7 @@ if ($rowCount != -1) {
$sql .= " LIMIT $limit_low,$limit_high";
}
-$sql = "SELECT S.*, DATE_FORMAT(timestamp, '%Y-%m-%d %T') AS date $sql";
+$sql = "SELECT S.*, DATE_FORMAT(timestamp, '".$config['dateformat']['mysql']['compact']."') AS date $sql";
foreach (dbFetchRows($sql,$param) as $syslog) {
$dev = device_by_id_cache($syslog['device_id']);
diff --git a/html/pages/authlog.inc.php b/html/pages/authlog.inc.php
index 606345526..64b4080ad 100644
--- a/html/pages/authlog.inc.php
+++ b/html/pages/authlog.inc.php
@@ -4,7 +4,7 @@ if ($_SESSION['userlevel'] >= '10')
{
echo("
");
- foreach (dbFetchRows("SELECT *,DATE_FORMAT(datetime, '%D %b %Y %T') as humandate FROM `authlog` ORDER BY `datetime` DESC LIMIT 0,250") as $entry)
+ foreach (dbFetchRows("SELECT *,DATE_FORMAT(datetime, '".$config['dateformat']['mysql']['compact']."') as humandate FROM `authlog` ORDER BY `datetime` DESC LIMIT 0,250") as $entry)
{
if ($bg == $list_colour_a) { $bg = $list_colour_b; } else { $bg=$list_colour_a; }
diff --git a/html/pages/bill.inc.php b/html/pages/bill.inc.php
index c6c0029e6..13b640057 100644
--- a/html/pages/bill.inc.php
+++ b/html/pages/bill.inc.php
@@ -50,8 +50,8 @@ if (bill_permitted($bill_id))
$bill_color = "#0000cc";
}
- $fromtext = dbFetchCell("SELECT DATE_FORMAT($datefrom, '%M %D %Y')");
- $totext = dbFetchCell("SELECT DATE_FORMAT($dateto, '%M %D %Y')");
+ $fromtext = dbFetchCell("SELECT DATE_FORMAT($datefrom, '".$config['dateformat']['mysql']['date']."')");
+ $totext = dbFetchCell("SELECT DATE_FORMAT($dateto, '".$config['dateformat']['mysql']['date']."')");
$unixfrom = dbFetchCell("SELECT UNIX_TIMESTAMP('$datefrom')");
$unixto = dbFetchCell("SELECT UNIX_TIMESTAMP('$dateto')");
diff --git a/html/pages/bill/transfer.inc.php b/html/pages/bill/transfer.inc.php
index fd317393d..194db3446 100644
--- a/html/pages/bill/transfer.inc.php
+++ b/html/pages/bill/transfer.inc.php
@@ -25,8 +25,8 @@
$in_data = $bill_data['total_data_in'];
$out_data = $bill_data['total_data_out'];
- $fromtext = dbFetchCell("SELECT DATE_FORMAT($datefrom, '%M %D %Y')");
- $totext = dbFetchCell("SELECT DATE_FORMAT($dateto, '%M %D %Y')");
+ $fromtext = dbFetchCell("SELECT DATE_FORMAT($datefrom, '".$config['dateformat']['mysql']['date']."')");
+ $totext = dbFetchCell("SELECT DATE_FORMAT($dateto, '".$config['dateformat']['mysql']['date']."')");
$unixfrom = dbFetchCell("SELECT UNIX_TIMESTAMP('$datefrom')");
$unixto = dbFetchCell("SELECT UNIX_TIMESTAMP('$dateto')");
$unix_prev_from = dbFetchCell("SELECT UNIX_TIMESTAMP('$lastfrom')");
diff --git a/html/pages/device/logs/eventlog.inc.php b/html/pages/device/logs/eventlog.inc.php
index 57753f048..aec8bdd98 100644
--- a/html/pages/device/logs/eventlog.inc.php
+++ b/html/pages/device/logs/eventlog.inc.php
@@ -31,7 +31,7 @@ if( !empty($_POST['string']) ) {
$sql .= " AND message LIKE '%".mres($_POST['string'])."%'";
}
-$entries = dbFetchRows("SELECT *,DATE_FORMAT(datetime, '%D %b %Y %T') as humandate FROM `eventlog` WHERE `host` = ? $sql ORDER BY `datetime` DESC LIMIT 0,250", array($device['device_id']));
+$entries = dbFetchRows("SELECT *,DATE_FORMAT(datetime, '".$config['dateformat']['mysql']['compact']."') as humandate FROM `eventlog` WHERE `host` = ? $sql ORDER BY `datetime` DESC LIMIT 0,250", array($device['device_id']));
echo('
diff --git a/html/pages/device/logs/syslog.inc.php b/html/pages/device/logs/syslog.inc.php
index c3abdad80..5bd715912 100644
--- a/html/pages/device/logs/syslog.inc.php
+++ b/html/pages/device/logs/syslog.inc.php
@@ -40,7 +40,7 @@ if ($_POST['program'])
$param[] = $_POST['program'];
}
-$sql = "SELECT *, DATE_FORMAT(timestamp, '%Y-%m-%d %T') AS date from syslog WHERE device_id = ? $where";
+$sql = "SELECT *, DATE_FORMAT(timestamp, '".$config['dateformat']['mysql']['compact']."') AS date from syslog WHERE device_id = ? $where";
$sql .= " ORDER BY timestamp DESC LIMIT 1000";
echo('
diff --git a/html/pages/device/overview/eventlog.inc.php b/html/pages/device/overview/eventlog.inc.php
index 1fb306c67..ff235aa4a 100644
--- a/html/pages/device/overview/eventlog.inc.php
+++ b/html/pages/device/overview/eventlog.inc.php
@@ -10,7 +10,7 @@ echo("

Recent Events");
echo('
');
-$eventlog = dbFetchRows("SELECT *,DATE_FORMAT(datetime, '%d/%b/%y %T') as humandate FROM `eventlog` WHERE `host` = ? ORDER BY `datetime` DESC LIMIT 0,10", array($device['device_id']));
+$eventlog = dbFetchRows("SELECT *,DATE_FORMAT(datetime, '".$config['dateformat']['mysql']['compact']."') as humandate FROM `eventlog` WHERE `host` = ? ORDER BY `datetime` DESC LIMIT 0,10", array($device['device_id']));
foreach ($eventlog as $entry)
{
include("includes/print-event-short.inc.php");
diff --git a/html/pages/device/overview/syslog.inc.php b/html/pages/device/overview/syslog.inc.php
index b473078a5..1d26fbb2a 100644
--- a/html/pages/device/overview/syslog.inc.php
+++ b/html/pages/device/overview/syslog.inc.php
@@ -2,7 +2,7 @@
if ($config['enable_syslog'])
{
- $syslog = dbFetchRows("SELECT *, DATE_FORMAT(timestamp, '%Y-%m-%d %T') AS date from syslog WHERE device_id = ? ORDER BY timestamp DESC LIMIT 20", array($device['device_id']));
+ $syslog = dbFetchRows("SELECT *, DATE_FORMAT(timestamp, '".$config['dateformat']['mysql']['compact']."') AS date from syslog WHERE device_id = ? ORDER BY timestamp DESC LIMIT 20", array($device['device_id']));
if (count($syslog))
{
echo('');
diff --git a/html/pages/device/port/events.inc.php b/html/pages/device/port/events.inc.php
index 3cdd09c7b..6de49ef26 100644
--- a/html/pages/device/port/events.inc.php
+++ b/html/pages/device/port/events.inc.php
@@ -1,6 +1,6 @@
');
foreach ($entries as $entry)
diff --git a/html/pages/front/default.php b/html/pages/front/default.php
index 9e689b006..7b17f08a4 100644
--- a/html/pages/front/default.php
+++ b/html/pages/front/default.php
@@ -165,7 +165,7 @@ echo('
if ($config['enable_syslog'])
{
- $sql = "SELECT *, DATE_FORMAT(timestamp, '%D %b %T') AS date from syslog ORDER BY timestamp DESC LIMIT 20";
+ $sql = "SELECT *, DATE_FORMAT(timestamp, '".$config['dateformat']['mysql']['compact']."') AS date from syslog ORDER BY timestamp DESC LIMIT 20";
$query = mysql_query($sql);
echo('
@@ -198,10 +198,10 @@ if ($config['enable_syslog'])
if ($_SESSION['userlevel'] >= '10')
{
- $query = "SELECT *,DATE_FORMAT(datetime, '%D %b %T') as humandate FROM `eventlog` ORDER BY `datetime` DESC LIMIT 0,15";
+ $query = "SELECT *,DATE_FORMAT(datetime, '".$config['dateformat']['mysql']['compact']."') as humandate FROM `eventlog` ORDER BY `datetime` DESC LIMIT 0,15";
$alertquery = "SELECT devices.device_id,name,state,time_logged FROM alert_log LEFT JOIN devices ON alert_log.device_id=devices.device_id LEFT JOIN alert_rules ON alert_log.rule_id=alert_rules.id ORDER BY `time_logged` DESC LIMIT 0,15";
} else {
- $query = "SELECT *,DATE_FORMAT(datetime, '%D %b %T') as humandate FROM `eventlog` AS E, devices_perms AS P WHERE E.host = P.device_id AND P.user_id = " . $_SESSION['user_id'] . " ORDER BY `datetime` DESC LIMIT 0,15";
+ $query = "SELECT *,DATE_FORMAT(datetime, '".$config['dateformat']['mysql']['compact']."') as humandate FROM `eventlog` AS E, devices_perms AS P WHERE E.host = P.device_id AND P.user_id = " . $_SESSION['user_id'] . " ORDER BY `datetime` DESC LIMIT 0,15";
$alertquery = "SELECT devices.device_id,name,state,time_logged FROM alert_log LEFT JOIN devices ON alert_log.device_id=devices.device_id LEFT JOIN alert_rules ON alert_log.rule_id=alert_rules.id RIGHT JOIN devices_perms ON alert_log.device_id = devices_perms.device_id AND devices_perms.user_id = " . $_SESSION['user_id'] . " ORDER BY `time_logged` DESC LIMIT 0,15";
}
diff --git a/html/pages/front/demo.php b/html/pages/front/demo.php
index f9651a58e..1000fbca2 100644
--- a/html/pages/front/demo.php
+++ b/html/pages/front/demo.php
@@ -161,7 +161,7 @@ echo("
Recent Syslog Messages
");
-$sql = "SELECT *, DATE_FORMAT(timestamp, '%D %b %T') AS date from `syslog` ORDER BY seq DESC LIMIT 20";
+$sql = "SELECT *, DATE_FORMAT(timestamp, '".$config['dateformat']['mysql']['compact']."') AS date from `syslog` ORDER BY seq DESC LIMIT 20";
echo("
");
foreach (dbFetchRows($sql) as $entry)
{
diff --git a/html/pages/front/example2.php b/html/pages/front/example2.php
index 1950e9eee..717ee7518 100644
--- a/html/pages/front/example2.php
+++ b/html/pages/front/example2.php
@@ -96,7 +96,7 @@ echo("
");
-$sql = "SELECT *, DATE_FORMAT(timestamp, '%D %b %T') AS date from syslog ORDER BY timestamp DESC LIMIT 20";
+$sql = "SELECT *, DATE_FORMAT(timestamp, '".$config['dateformat']['mysql']['compact']."') AS date from syslog ORDER BY timestamp DESC LIMIT 20";
echo("");
foreach (dbFetchRows($sql) as $entry)
{
diff --git a/html/pages/front/globe.php b/html/pages/front/globe.php
index badedcd68..8218d302c 100644
--- a/html/pages/front/globe.php
+++ b/html/pages/front/globe.php
@@ -123,7 +123,7 @@ echo '
//From default.php - This code is not part of above license.
if ($config['enable_syslog']) {
-$sql = "SELECT *, DATE_FORMAT(timestamp, '%D %b %T') AS date from syslog ORDER BY seq DESC LIMIT 20";
+$sql = "SELECT *, DATE_FORMAT(timestamp, '".$config['dateformat']['mysql']['compact']."') AS date from syslog ORDER BY seq DESC LIMIT 20";
$query = mysql_query($sql);
echo('
@@ -155,9 +155,9 @@ echo('
if ($_SESSION['userlevel'] == '10')
{
- $query = "SELECT *,DATE_FORMAT(datetime, '%D %b %T') as humandate FROM `eventlog` ORDER BY `datetime` DESC LIMIT 0,15";
+ $query = "SELECT *,DATE_FORMAT(datetime, '".$config['dateformat']['mysql']['compact']."') as humandate FROM `eventlog` ORDER BY `datetime` DESC LIMIT 0,15";
} else {
- $query = "SELECT *,DATE_FORMAT(datetime, '%D %b %T') as humandate FROM `eventlog` AS E, devices_perms AS P WHERE E.host =
+ $query = "SELECT *,DATE_FORMAT(datetime, '".$config['dateformat']['mysql']['compact']."') as humandate FROM `eventlog` AS E, devices_perms AS P WHERE E.host =
P.device_id AND P.user_id = " . $_SESSION['user_id'] . " ORDER BY `datetime` DESC LIMIT 0,15";
}
diff --git a/html/pages/front/jt.php b/html/pages/front/jt.php
index bead12a52..2256cf085 100644
--- a/html/pages/front/jt.php
+++ b/html/pages/front/jt.php
@@ -104,7 +104,7 @@ echo("
");
-$sql = "SELECT *, DATE_FORMAT(timestamp, '%D %b %T') AS date from syslog,devices WHERE syslog.device_id = devices.device_id ORDER BY seq DESC LIMIT 20";
+$sql = "SELECT *, DATE_FORMAT(timestamp, '".$config['dateformat']['mysql']['compact']."') AS date from syslog,devices WHERE syslog.device_id = devices.device_id ORDER BY seq DESC LIMIT 20";
echo("
");
foreach (dbFetchRows($sql) as $entry)
{
diff --git a/html/pages/front/traffic.php b/html/pages/front/traffic.php
index da614b1fb..5f9a8acce 100644
--- a/html/pages/front/traffic.php
+++ b/html/pages/front/traffic.php
@@ -98,7 +98,7 @@ echo("
");
-$sql = "SELECT *, DATE_FORMAT(timestamp, '%D %b %T') AS date from syslog,devices WHERE syslog.device_id = devices.device_id ORDER BY seq DESC LIMIT 20";
+$sql = "SELECT *, DATE_FORMAT(timestamp, '".$config['dateformat']['mysql']['compact']."') AS date from syslog,devices WHERE syslog.device_id = devices.device_id ORDER BY seq DESC LIMIT 20";
echo("");
foreach (dbFetchRows($sql) as $entry)
{
diff --git a/includes/defaults.inc.php b/includes/defaults.inc.php
index fc56cb2aa..2331c6923 100644
--- a/includes/defaults.inc.php
+++ b/includes/defaults.inc.php
@@ -640,6 +640,11 @@ $config['dateformat']['long'] = "r"; # RFC2822 style
$config['dateformat']['compact'] = "Y-m-d H:i:s";
$config['dateformat']['time'] = "H:i:s";
+# Date format for MySQL DATE_FORMAT
+$config['dateformat']['mysql']['compact'] = "%Y-%m-%d %H:%i:%s";
+$config['dateformat']['mysql']['date'] = "%Y-%m-%d";
+$config['dateformat']['mysql']['time'] = "%H:%i:%s";
+
$config['enable_clear_discovery'] = 1;// Set this to 0 if you want to disable the web option to rediscover devices
$config['enable_port_relationship'] = TRUE;// Set this to false to not display neighbour relationships for ports
diff --git a/scripts/console-ui.php b/scripts/console-ui.php
index 566b94dd4..2ab7b7368 100755
--- a/scripts/console-ui.php
+++ b/scripts/console-ui.php
@@ -52,7 +52,7 @@ while($end == 0)
{
$sql = "WHERE host='".$options['d']."'";
}
- $query = "SELECT *,DATE_FORMAT(datetime, '%D %b %Y %T') as humandate FROM `eventlog` AS E $sql ORDER BY `datetime` DESC LIMIT 20";
+ $query = "SELECT *,DATE_FORMAT(datetime, '".$config['dateformat']['mysql']['compact']."') as humandate FROM `eventlog` AS E $sql ORDER BY `datetime` DESC LIMIT 20";
foreach (dbFetchRows($query, $param) as $entry)
{
$tbl->addRow(array($entry['datetime'],gethostbyid($entry['host']),$entry['message'],$entry['type'],$entry['reference']));
@@ -67,7 +67,7 @@ while($end == 0)
{
$sql = "WHERE device_id='".$options['d']."'";
}
- $query = "SELECT *, DATE_FORMAT(timestamp, '%Y-%m-%d %T') AS date from syslog AS S $sql_query ORDER BY `timestamp` DESC LIMIT 20";
+ $query = "SELECT *, DATE_FORMAT(timestamp, '".$config['dateformat']['mysql']['compact']."') AS date from syslog AS S $sql_query ORDER BY `timestamp` DESC LIMIT 20";
foreach (dbFetchRows($query, $param) as $entry)
{
$tbl->addRow(array($entry['timestamp'],gethostbyid($entry['device_id']),$entry['program'],$entry['msg'],$entry['level'],$entry['facility']));