Ho letto il manuale di php per la funzione time, mi chiedevo se oltre a stampare il timestamp in formato italiano, è possibile anche stampare dopo 24 la dicitura ad esempio "1 giorno fa" poi dopo una settimana "1 settimana fa" ecc
Ho letto il manuale di php per la funzione time, mi chiedevo se oltre a stampare il timestamp in formato italiano, è possibile anche stampare dopo 24 la dicitura ad esempio "1 giorno fa" poi dopo una settimana "1 settimana fa" ecc
Io utilizzo una funzione che avevo trovato qui, che ho tradotto e adattato per funzionare in italiano,.
codice:function _ago($tm,$rcs = 0) { $cur_tm = time(); $dif = $cur_tm-$tm; $pds = array('secondo','minuto','ora','giorno','settimana','mese','anno','secolo'); $pdsp = array('secondi', 'minuti', 'ore', 'giorni', 'settimane', 'mesi', 'anni', 'secoli'); $lngh = array(1,60,3600,86400,604800,2630880,31570560,315705600); for($v = sizeof($lngh)-1; ($v >= 0)&&(($no = $dif/$lngh[$v])<=1); $v--); if($v < 0) $v = 0; $_tm = $cur_tm-($dif%$lngh[$v]); $no = floor($no); if($no <> 1) $pds[$v] = $pdsp[$v]; $x = sprintf("%d %s ",$no,$pds[$v]); if(($rcs == 1)&&($v >= 1)&&(($cur_tm-$_tm) > 0)) $x .= time_ago($_tm); $x .= " fa"; return $x; }
Ultima modifica di livellacri; 18-06-2017 a 10:17
un esempio usando datetime:
Codice PHP:
function calculate_days_difference($datetime_start, $datetime_to_compare) {
$diff = $datetime_start->diff($datetime_to_compare);
$diff_days = $diff->format("%a");
$diff_sign = $diff->format("%R");
switch (true) {
case $diff_days > 1:
$day_tag = "giorni";
break;
case $diff_days == 1:
$day_tag = "giorni";
break;
case $diff_days == 0 and $diff_sign == "-":
return "oggi";
break;
case $diff_days == 0 and $diff_sign == "+":
$day_tag = "giorno";
break;
default:
return "qualcosa è andato storto";
break;
}
switch ($diff_sign) {
case '-':
return "$diff_days $day_tag fa";
case '+':
$diff_days += 1;
return "tra $diff_days $day_tag";
default:
return false;
}
}
$datetimezone = new DateTimeZone("UTC");
$now = new DateTime("now", $datetimezone);
$date_to_compare = new DateTime("2017-06-17", $datetimezone);
$diff = $now->diff($date_to_compare);
echo "inizio: " . $now->format("Y-m-d H:i:s") . "\n";
echo "fine: " . $date_to_compare->format("Y-m-d H:i:s") . "\n";
echo $diff->format("%R%a giorni %h ore") . "\n";
echo calculate_days_difference($now, $date_to_compare) . "\n";
?>
Ecco il codice che ho composto, visto che lei ha fatto il tirchio a non darmelo, era uno script semplicissimo da comporre, inoltre voglio dirle che questi tipi di codici si trovano in giro su Internet facilmente, non come lei reputa 'introvabili'. Cordiali Saluti.
Codice PHP:
function getCreationDateTimeSince($timestamp){
$totaldelay = time() - strtotime($timestamp); {
if($years=floor($totaldelay/31536000)) {
$totaldelay = $totaldelay % 31536000;
$getYear = '';
if ($years > 1)
$getYear=' anni fa';
else{
$getYear=' anno fa';
}
$timesince = $timesince.$years.$getYear;
return $timesince; }
if($months=floor($totaldelay/2628000))
{
$totaldelay = $totaldelay % 2628000;
$getMonth = ''; if ($months > 1)
$getMonth=' mesi fa';
else{
$getMonth=' mese fa';
}
$timesince = $timesince.$months.$getMonth;
return $timesince;
}
if($days=floor($totaldelay/86400))
{
$totaldelay = $totaldelay % 86400;
$getDay = '';
if ($days > 1)
$getDay=' giorni fa';
else{
$getDay=' giorno fa';
}
$timesince = $timesince.$days.$getDay; return $timesince; }
if($hours=floor($totaldelay/3600)) { $totaldelay = $totaldelay % 3600; $getHour = ''; if ($hours > 1) $getHour=' ore fa'; else{ $getHour=' ora fa'; } $timesince = $timesince.$hours.$getHour; return $timesince; } if($minutes=floor($totaldelay/60)) { $totaldelay = $totaldelay % 60; $getMinute = ''; if ($minutes > 1) $getMinute=' minuti fa'; else{ $getMinute=' minuto fa'; } $timesince = $timesince.$minutes.$getMinute; return $timesince; } if($seconds=floor($totaldelay/1)) { $totaldelay = $totaldelay % 1; $getSecond = ''; if ($seconds > 1) $getSecond=' secondi fa'; else{ $getSecond=' secondo fa'; } $timesince = $timesince.$seconds.$getSecond; return $timesince; } }}
Ultima modifica di Kitt3000; 20-06-2017 a 13:08