Commit 17ff9c54 authored by Martyn Smith's avatar Martyn Smith Committed by Martyn Smith
Browse files

added function for getting unix timestamps out of the db

parent 134dcc18
......@@ -1123,6 +1123,36 @@ function db_format_timestamp($ts) {
return $db->DBTimeStamp($ts);
}
/**
* Given a field name, this returns a function call suitable for the current
* database to return a unix timestamp
*
* @param field the field to apply the function to
* @param as what to name the field (optional, defaults to $field)
*
* @return timestamp string in database timestamp format
*/
function db_format_tsfield($field, $as = null) {
$tsfield = '';
if (is_postgres()) {
$tsfield = "FLOOR(EXTRACT(EPOCH FROM $field))";
}
else if (is_mysql()) {
$tsfield = "UNIX_TIMESTAMP($field)";
}
else {
throw new DatalibException('db_format_tsfield() is not implemented for your database engine (' . get_config('dbtype') . ')');
}
if ($as === null) {
$as = $field;
}
$tsfield .= " AS $as";
return $tsfield;
}
/**
* This function, called from setup.php includes all the configuration
* needed to properly work agains any DB. It setups connection encoding
......@@ -1140,4 +1170,8 @@ function is_postgres() {
return (strpos(get_config('dbtype'), 'postgres') === 0);
}
function is_mysql() {
return (strpos(get_config('dbtype'), 'mysql') === 0);
}
?>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment