Consistent date formatting from MySQL

This commit is contained in:
laf
2015-05-22 13:38:52 +01:00
parent a5048cf185
commit 6496e6ece4
22 changed files with 53 additions and 28 deletions

View File

@ -38,3 +38,23 @@ place.
As an example pull request, see [PR 706](https://github.com/librenms/librenms/pull/706/files) to get an idea of what 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. 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";
```

View File

@ -32,7 +32,7 @@ $pdf->AddPage('L');
$numresults = 250; $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) { foreach (dbFetchRows($full_query, $param) as $alert_entry) {
$hostname = gethostbyid(mres($alert_entry['device_id'])); $hostname = gethostbyid(mres($alert_entry['device_id']));

View File

@ -46,7 +46,7 @@ if ($rowCount != -1) {
$sql .= " LIMIT $limit_low,$limit_high"; $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) { foreach (dbFetchRows($sql,$param) as $schedule) {
$status = 0; $status = 0;

View File

@ -39,7 +39,7 @@ if ($rowCount != -1) {
$sql .= " LIMIT $limit_low,$limit_high"; $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; $rulei = 0;
foreach (dbFetchRows($sql,$param) as $alertlog) { foreach (dbFetchRows($sql,$param) as $alertlog) {

View File

@ -46,7 +46,7 @@ if ($rowCount != -1) {
$sql .= " LIMIT $limit_low,$limit_high"; $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) { foreach (dbFetchRows($sql,$param) as $eventlog) {
$dev = device_by_id_cache($eventlog['host']); $dev = device_by_id_cache($eventlog['host']);

View File

@ -60,7 +60,7 @@ if ($rowCount != -1) {
$sql .= " LIMIT $limit_low,$limit_high"; $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) { foreach (dbFetchRows($sql,$param) as $syslog) {
$dev = device_by_id_cache($syslog['device_id']); $dev = device_by_id_cache($syslog['device_id']);

View File

@ -4,7 +4,7 @@ if ($_SESSION['userlevel'] >= '10')
{ {
echo("<table cellspacing=0 cellpadding=1 width=100%>"); echo("<table cellspacing=0 cellpadding=1 width=100%>");
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; } if ($bg == $list_colour_a) { $bg = $list_colour_b; } else { $bg=$list_colour_a; }

View File

@ -50,8 +50,8 @@ if (bill_permitted($bill_id))
$bill_color = "#0000cc"; $bill_color = "#0000cc";
} }
$fromtext = dbFetchCell("SELECT DATE_FORMAT($datefrom, '%M %D %Y')"); $fromtext = dbFetchCell("SELECT DATE_FORMAT($datefrom, '".$config['dateformat']['mysql']['date']."')");
$totext = dbFetchCell("SELECT DATE_FORMAT($dateto, '%M %D %Y')"); $totext = dbFetchCell("SELECT DATE_FORMAT($dateto, '".$config['dateformat']['mysql']['date']."')");
$unixfrom = dbFetchCell("SELECT UNIX_TIMESTAMP('$datefrom')"); $unixfrom = dbFetchCell("SELECT UNIX_TIMESTAMP('$datefrom')");
$unixto = dbFetchCell("SELECT UNIX_TIMESTAMP('$dateto')"); $unixto = dbFetchCell("SELECT UNIX_TIMESTAMP('$dateto')");

View File

@ -25,8 +25,8 @@
$in_data = $bill_data['total_data_in']; $in_data = $bill_data['total_data_in'];
$out_data = $bill_data['total_data_out']; $out_data = $bill_data['total_data_out'];
$fromtext = dbFetchCell("SELECT DATE_FORMAT($datefrom, '%M %D %Y')"); $fromtext = dbFetchCell("SELECT DATE_FORMAT($datefrom, '".$config['dateformat']['mysql']['date']."')");
$totext = dbFetchCell("SELECT DATE_FORMAT($dateto, '%M %D %Y')"); $totext = dbFetchCell("SELECT DATE_FORMAT($dateto, '".$config['dateformat']['mysql']['date']."')");
$unixfrom = dbFetchCell("SELECT UNIX_TIMESTAMP('$datefrom')"); $unixfrom = dbFetchCell("SELECT UNIX_TIMESTAMP('$datefrom')");
$unixto = dbFetchCell("SELECT UNIX_TIMESTAMP('$dateto')"); $unixto = dbFetchCell("SELECT UNIX_TIMESTAMP('$dateto')");
$unix_prev_from = dbFetchCell("SELECT UNIX_TIMESTAMP('$lastfrom')"); $unix_prev_from = dbFetchCell("SELECT UNIX_TIMESTAMP('$lastfrom')");

View File

@ -31,7 +31,7 @@ if( !empty($_POST['string']) ) {
$sql .= " AND message LIKE '%".mres($_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(' <div class="panel panel-default panel-condensed"> echo(' <div class="panel panel-default panel-condensed">
<div class="panel-heading"> <div class="panel-heading">

View File

@ -40,7 +40,7 @@ if ($_POST['program'])
$param[] = $_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"; $sql .= " ORDER BY timestamp DESC LIMIT 1000";
echo(' <div class="panel panel-default panel-condensed"> echo(' <div class="panel panel-default panel-condensed">
<div class="panel-heading"> <div class="panel-heading">

View File

@ -10,7 +10,7 @@ echo("<img src='images/16/report.png'> Recent Events</a>");
echo(' </div> echo(' </div>
<table class="table table-hover table-condensed table-striped">'); <table class="table table-hover table-condensed table-striped">');
$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) foreach ($eventlog as $entry)
{ {
include("includes/print-event-short.inc.php"); include("includes/print-event-short.inc.php");

View File

@ -2,7 +2,7 @@
if ($config['enable_syslog']) 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)) if (count($syslog))
{ {
echo('<div class="container-fluid">'); echo('<div class="container-fluid">');

View File

@ -1,6 +1,6 @@
<?php <?php
$entries = dbFetchRows("SELECT *,DATE_FORMAT(datetime, '%D %b %Y %T') as humandate FROM `eventlog` WHERE `host` = ? AND `type` = 'interface' AND `reference` = '".$port['port_id']."' 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` = ? AND `type` = 'interface' AND `reference` = '".$port['port_id']."' ORDER BY `datetime` DESC LIMIT 0,250", array($device['device_id']));
echo('<table cellspacing="0" cellpadding="2" width="100%">'); echo('<table cellspacing="0" cellpadding="2" width="100%">');
foreach ($entries as $entry) foreach ($entries as $entry)

View File

@ -165,7 +165,7 @@ echo('
if ($config['enable_syslog']) 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); $query = mysql_query($sql);
echo('<div class="container-fluid"> echo('<div class="container-fluid">
@ -198,10 +198,10 @@ if ($config['enable_syslog'])
if ($_SESSION['userlevel'] >= '10') 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"; $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 { } 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"; $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";
} }

View File

@ -161,7 +161,7 @@ echo("
<h3>Recent Syslog Messages</h3> <h3>Recent Syslog Messages</h3>
"); ");
$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("<table cellspacing=0 cellpadding=2 width=100%>"); echo("<table cellspacing=0 cellpadding=2 width=100%>");
foreach (dbFetchRows($sql) as $entry) foreach (dbFetchRows($sql) as $entry)
{ {

View File

@ -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("<table cellspacing=0 cellpadding=2 width=100%>"); echo("<table cellspacing=0 cellpadding=2 width=100%>");
foreach (dbFetchRows($sql) as $entry) foreach (dbFetchRows($sql) as $entry)
{ {

View File

@ -123,7 +123,7 @@ echo ' </div>
//From default.php - This code is not part of above license. //From default.php - This code is not part of above license.
if ($config['enable_syslog']) { 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); $query = mysql_query($sql);
echo('<div class="container-fluid"> echo('<div class="container-fluid">
<div class="row"> <div class="row">
@ -155,9 +155,9 @@ echo('<div class="container-fluid">
if ($_SESSION['userlevel'] == '10') 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 { } 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"; P.device_id AND P.user_id = " . $_SESSION['user_id'] . " ORDER BY `datetime` DESC LIMIT 0,15";
} }

View File

@ -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("<table cellspacing=0 cellpadding=2 width=100%>"); echo("<table cellspacing=0 cellpadding=2 width=100%>");
foreach (dbFetchRows($sql) as $entry) foreach (dbFetchRows($sql) as $entry)
{ {

View File

@ -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("<table cellspacing=0 cellpadding=2 width=100%>"); echo("<table cellspacing=0 cellpadding=2 width=100%>");
foreach (dbFetchRows($sql) as $entry) foreach (dbFetchRows($sql) as $entry)
{ {

View File

@ -640,6 +640,11 @@ $config['dateformat']['long'] = "r"; # RFC2822 style
$config['dateformat']['compact'] = "Y-m-d H:i:s"; $config['dateformat']['compact'] = "Y-m-d H:i:s";
$config['dateformat']['time'] = "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_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 $config['enable_port_relationship'] = TRUE;// Set this to false to not display neighbour relationships for ports

View File

@ -52,7 +52,7 @@ while($end == 0)
{ {
$sql = "WHERE host='".$options['d']."'"; $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) foreach (dbFetchRows($query, $param) as $entry)
{ {
$tbl->addRow(array($entry['datetime'],gethostbyid($entry['host']),$entry['message'],$entry['type'],$entry['reference'])); $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']."'"; $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) foreach (dbFetchRows($query, $param) as $entry)
{ {
$tbl->addRow(array($entry['timestamp'],gethostbyid($entry['device_id']),$entry['program'],$entry['msg'],$entry['level'],$entry['facility'])); $tbl->addRow(array($entry['timestamp'],gethostbyid($entry['device_id']),$entry['program'],$entry['msg'],$entry['level'],$entry['facility']));