Convert Oracle Date to UNIX Time
Oracle unfortunately does not have a UNIX conversion function. However, you can do it mathematically in SQL. I’ve written a function in PHP that will convert a date field to a UNIX timestamp in Oracle.
/**
* Inserts the SQL necessary to format a date to a UNIX time in Oracle.
* @param string Field name to convert.
* @param string (Optional) Field Alias
* @return string The SQL necessary to convert to UNIX time.
*/
function toUnixTime($field, $fieldAlias=null){
if($fieldAlias== null){
$fieldAlias= $field;
}
return "(ROUND((".$field." - to_date('01-01-1970', 'DD-MM-YYYY')) * (86400) + (28800)) ".$fieldAlias;
}
Usage:
$sql = “SELECT “.toUnixTime(“created”).” FROM users”;
or
//CreatedOn alias
$sql = “SELECT “.toUnixTime(“created”, “createdOn”).” FROM users”;
0 Comments
No Comments »
No comments yet.
RSS feed for comments on this post. | TrackBack URI
