diff --git a/ecrire/base/abstract_sql.php b/ecrire/base/abstract_sql.php
index 5ddc2ee18d36a4dfdee9b9edbc084808a3d5caaf..10a53798fe6f82740a216da5eed32cf7ce6b7556 100644
--- a/ecrire/base/abstract_sql.php
+++ b/ecrire/base/abstract_sql.php
@@ -352,13 +352,18 @@ function calcul_mysql_in($val, $valeurs, $not='') {
 // http://doc.spip.org/@test_sql_int
 function test_sql_int($type)
 {
-	return (strpos($type, 'bigint') === 0
-	OR strpos($type, 'int') === 0
-	OR strpos($type, 'tinyint') === 0);
+  return (preg_match('/^bigint/i',$type)
+	  OR preg_match('/^int/i',$type)
+	  OR preg_match('/^tinyint/i',$type));
 }
 
+function test_sql_date($type)
+{
+  return (preg_match('/^datetime/i',$type)
+	  OR preg_match('/^timestamp/i',$type));
+}
 
-// Cette fonction est vouee a disparaitre
+// Cette fonction devrait disparaitre
 
 // http://doc.spip.org/@description_table
 function description_table($nom){
diff --git a/ecrire/public/criteres.php b/ecrire/public/criteres.php
index 1034176c36027eab278ccf467308a2164546be67..b9af45d2c5c3dfab48caa6267bd34f6f4e121cd7 100644
--- a/ecrire/public/criteres.php
+++ b/ecrire/public/criteres.php
@@ -344,12 +344,9 @@ function critere_parinverse($idb, &$boucles, $crit, $sens='') {
 	  if ($tri[0]->type != 'texte') {
 	  	// calculer le order dynamique qui verifie les champs
 			$order = calculer_critere_arg_dynamique($idb, $boucles, $tri, $sens);
-	    // et ajouter un champ hasard dans le select pour supporter 'hasard' comme tri dynamique
-			if (sql_select(array("RAND()")))
-				$par = "RAND()";
-			else
-				$par = "MOD(".$boucle->id_table.'.'.$boucle->primary
-			  ." * UNIX_TIMESTAMP(),32767) & UNIX_TIMESTAMP()";
+			// et ajouter un champ hasard dans le select 
+			//pour supporter 'hasard' comme tri dynamique
+			$par = "UNIX_TIMESTAMP()";
 			$boucle->select[]= $par . " AS hasard";
 	  } else {
 	      $par = array_shift($tri);
@@ -375,13 +372,7 @@ function critere_parinverse($idb, &$boucles, $crit, $sens='') {
 		if (count($match)>2) { $par = substr($match[2],1,-1); $fct = $match[1]; }
 	// par hasard
 		if ($par == 'hasard') {
-		// tester si cette version de MySQL accepte la commande RAND()
-		// sinon faire un gloubi-boulga maison avec de la mayonnaise.
-		  if (sql_select(array("RAND()")))
-			$par = "RAND()";
-		  else
-			$par = "MOD(".$boucle->id_table.'.'.$boucle->primary
-			  ." * UNIX_TIMESTAMP(),32767) & UNIX_TIMESTAMP()";
+			$par = "UNIX_TIMESTAMP()";
 		  $boucle->select[]= $par . " AS alea";
 		  $order = "'alea'";
 		}
diff --git a/ecrire/req/mysql.php b/ecrire/req/mysql.php
index 6ce297d91046807621e114bdf1bcd3d85743d98b..a7c04a8b4e0c2c04d4041bdc35123ae807242fff 100644
--- a/ecrire/req/mysql.php
+++ b/ecrire/req/mysql.php
@@ -553,8 +553,7 @@ function spip_release_lock($nom) {
 
 // http://doc.spip.org/@spip_mysql_cite
 function spip_mysql_cite($val, $type) {
-	if(((strpos($type, 'datetime')===0) OR strpos($type, 'TIMESTAMP')===0)
-	 AND preg_match('/^\w+\(/', $val))
+	if (test_sql_date($type) AND preg_match('/^\w+\(/', $val))
 		return $val;
 	return _q($val);
 }
diff --git a/ecrire/req/pg.php b/ecrire/req/pg.php
index abe5bf51192a6c0de8b60c9eee375d8b3a735a54..d87fd70137d6463055027f9aa5147cc19107e4a0 100644
--- a/ecrire/req/pg.php
+++ b/ecrire/req/pg.php
@@ -340,6 +340,9 @@ function spip_pg_frommysql($arg)
 	$res = preg_replace('/\b0[+]([^, ]+)\s*/',
 			    'CAST(substring(\1, \'^ *[0-9]+\') as int)',
 			    $res);
+	$res = preg_replace('/UNIX_TIMESTAMP\s*[(]\s*[)]/',
+			    'EXTRACT(\'epoch\' FROM NOW())', $res);
+
 	$res = preg_replace('/UNIX_TIMESTAMP\s*[(]([^)]*)[)]/',
 			    'EXTRACT(\'epoch\' FROM \1)', $res);
 
@@ -627,7 +630,7 @@ function spip_pg_sequence($table)
 // http://doc.spip.org/@spip_pg_cite
 function spip_pg_cite($v, $t)
 {
-	if ((strpos($t, 'datetime')===0) OR (strpos($t, 'TIMESTAMP')===0)) {
+	if (test_sql_date($t)) {
 		if (strpos("0123456789", $v[0]) === false)
 			return spip_pg_frommysql($v);
 		else {