paguira
Messages postés
12
Date d'inscription
lundi 8 janvier 2007
Statut
Membre
Dernière intervention
1 octobre 2008
19 janv. 2008 à 18:26
Que pensez vous du script que je viens juste de faire ?
<?php
function convertSec($startDate, $endDate, $lang='en'){
// Extraction des secondes, minutes, heure, jours, mois, années
if (ereg("([0-9]{2,4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})", $startDate, $startregs)){
if(strlen($startregs[1]) <4) $startregs[1] = "20$startregs[1]";
if(strlen($startregs[2]) <2) $startregs[2] = "0$startregs[2]";
if(strlen($startregs[3]) <2) $startregs[3] = "0$startregs[3]";
if(strlen($startregs[4]) <2) $startregs[4] = "0$startregs[4]";
if(strlen($startregs[5]) <2) $startregs[5] = "0$startregs[5]";
if(strlen($startregs[6]) <2) $startregs[6] = "0$startregs[6]";
// timestamp
$startDateStamp = mktime($startregs[4], $startregs[5], $startregs[6], $startregs[2], $startregs[3], $startregs[1]);
}
// Extraction des secondes, minutes, heure, jours, mois, années
if (ereg("([0-9]{2,4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})", $endDate, $endregs)){
if(strlen($endregs[1]) <4) $endregs[1] = "20$endregs[1]";
if(strlen($endregs[2]) <2) $endregs[2] = "0$endregs[2]";
if(strlen($endregs[3]) <2) $endregs[3] = "0$endregs[3]";
if(strlen($endregs[4]) <2) $endregs[4] = "0$endregs[4]";
if(strlen($endregs[5]) <2) $endregs[5] = "0$endregs[5]";
if(strlen($endregs[6]) <2) $endregs[6] = "0$endregs[6]";
// timestamp
$endDateStamp = mktime($endregs[4], $endregs[5], $endregs[6], $endregs[2], $endregs[3], $endregs[1]);
}
$txt = '';
$time = $endDateStamp-$startDateStamp ;
if($startDate && $endDate && $lang){
// Les langues
switch($lang){
case 'fr':
$startTxt = 'Posté il y a ';
$startTxtAgo = 'Posté il y a';
$startTxtDate = 'Posté le ';
$stringSeconde = ' seconde';
$stringSecondes = ' secondes';
$stringMinute = ' minute';
$stringMinutes = ' minutes';
$stringHour = ' heure';
$stringHours = ' heures';
$stringYesterday = ' Posté hier à ';
$stringA = ' à ';
$stringAgo = '';
$stringAnd = ' et ';
break;
case 'en':
$startTxt = 'Posted ';
$startTxtAgo = 'Posted ago';
$startTxtDate = 'Posted on ';
$stringSeconde = ' seconde';
$stringSecondes = ' secondes';
$stringMinute = ' minute';
$stringMinutes = ' minutes';
$stringHour = ' hour';
$stringHours = ' hours';
$stringYesterday = ' Posted yesterday at ';
$stringA = ' at ';
$stringAgo = ' ago';
$stringAnd = ' and ';
break;
}
// une seconde if($time 1 && $startregs[3] $endregs[3]) $txt = $startTxt . $time . $stringSeconde . $stringAgo;
// texte en seconde
if($time < 60 && $startregs[3] == $endregs[3]) $txt = $startTxt . $time . $stringSecondes . $stringAgo;
// une minute if($time 60 && $startregs[3] $endregs[3]) $txt = $startTxt . '1' . $stringMinute . $stringAgo;
// texte en minute
if($time > 60 && $time < 3600 && $startregs[3] == $endregs[3]){
$Minute = $time/60;
$Minute = floor($Minute);
$txt = $startTxt . $Minute . $stringMinutes . $stringAgo;
}
// une heure if($time 3600) $txt $startTxt . '1' . $stringHour . $stringAgo;
// texte en heure et/ou minute
if($time >3600 && $startregs[3] == $endregs[3]){
$Hour = $time/3600;
$floorHour = floor($Hour);
$Minute = ($Hour-$floorHour)*60;
$floorMinute = floor($Minute);
switch($lang){
case 'fr':
if($Minute === 1){
if($Hour === 1){
$txt = $startTxt . $floorHour . $stringHour . $stringAnd . $floorMinute. $stringMinute;
}else{
$txt = $startTxt . $floorHour . $stringHours . $stringAnd . $floorMinute. $stringMinute;
}
}else if($Minute){
if($Hour === 1){
$txt = $startTxt . $floorHour . $stringHour . $stringAnd . $floorMinute. $stringMinutes;
}else{
$txt = $startTxt . $floorHour . $stringHours . $stringAnd . $floorMinute. $stringMinutes;
}
}else{
if($Hour === 1){
$txt = $startTxt . $floorHour . $stringHour;
}else{
$txt = $startTxt . $floorHour . $stringHours;
}
}
break;
case 'en':
if($Minute === 1){
if($Hour === 1){
$txt = $startTxtAgo . $floorHour . $stringHour . $stringAnd . $floorMinute. $stringMinute;
}else{
$txt = $startTxtAgo . $floorHour . $stringHours . $stringAnd . $floorMinute. $stringMinute;
}
}else if($Minute){
if($Hour === 1){
$txt = $startTxtAgo . $floorHour . $stringHour . $stringAnd . $floorMinute. $stringMinutes;
}else{
$txt = $startTxtAgo . $floorHour . $stringHours . $stringAnd . $florMinute. $stringMinutes;
}
}else{
if($Hour === 1){
$txt = $startTxt . $floorHour . $stringHour . $stringAgo;
}else{
$txt = $startTxt . $floorHour . $stringHours . $stringAgo;
}
}
break;
}
}
// la date si > 2 jour
if($time > 86400 && $startregs[3] != $endregs[3]){
switch($lang){
case 'fr':
$txt = $startTxtDate . $startregs[2] . '/' . $startregs[3] .'/'. $startregs[1] .' '. $stringA . $startregs[4] . ':' . $startregs[5];
break;
case 'en':
$txt = $startTxtDate . $startregs[3] . '/' . $startregs[2] .'/'. $startregs[1] .' '. $stringA . $startregs[4] . ':' . $startregs[5];
break;
}
// hier date > 1 jour et date < 2 jours
}else if( $time >=1 && $startregs[3] != $endregs[3]){
$txt = $stringYesterday . $startregs[4] . ':' . $startregs[5];
}
}
return $txt;
}
// Exemple :
$today = date('Y-m-d H:i:s');
$today = '2008-01-18 00:00:00';
$sql_date = '2008-01-17 23:59:59';
echo $today . '
';
echo $sql_date . '
';
echo '<hr />';
echo convertSec($sql_date, $today, 'fr');
echo '<hr />';
.?>