From c261148291c551f76d11227ac9ea883f5074d242 Mon Sep 17 00:00:00 2001 From: david Date: Thu, 1 Mar 2012 20:15:36 +0000 Subject: [PATCH] - Componente para convertir fechas en cadenas - Uso en grid de Usuario. git-svn-id: https://192.168.0.254/svn/Proyectos.Incam_IntranetNueva/trunk@22 77cfc57b-8ef4-1849-9df6-4a38aa5da120 --- www/protected/components/Time.php | 264 ++++++++++++++++++++++++++ www/protected/views/usuario/index.php | 2 +- 2 files changed, 265 insertions(+), 1 deletion(-) create mode 100644 www/protected/components/Time.php diff --git a/www/protected/components/Time.php b/www/protected/components/Time.php new file mode 100644 index 0000000..863f259 --- /dev/null +++ b/www/protected/components/Time.php @@ -0,0 +1,264 @@ +strtotime-parsable format, like MySQL's datetime datatype. + * + * Options: + * 'format' => a fall back format if the relative time is longer than the duration specified by end + * 'end' => The end of relative time telling + * + * Relative dates look something like this: + * 3 weeks, 4 days ago + * 15 seconds ago + * Formatted dates look like this: + * on 02/18/2004 + * + * The returned string includes 'ago' or 'on' and assumes you'll properly add a word + * like 'Posted ' before the function output. + * + * @param string $dateString Datetime string + * @param array $options Default format if timestamp is used in $dateString + * @return string Relative time string. + */ + function timeAgoInWords($dateTime, $options = array()) { + $now = time(); + + $inSeconds = strtotime($dateTime); + $backwards = ($inSeconds > $now); + + $format = 'j/n/y'; + $end = '+1 month'; + + if (is_array($options)) { + if (isset($options['format'])) { + $format = $options['format']; + unset($options['format']); + } + if (isset($options['end'])) { + $end = $options['end']; + unset($options['end']); + } + } else { + $format = $options; + } + + if ($backwards) { + $futureTime = $inSeconds; + $pastTime = $now; + } else { + $futureTime = $now; + $pastTime = $inSeconds; + } + $diff = $futureTime - $pastTime; + + // If more than a week, then take into account the length of months + if ($diff >= 604800) { + $current = array(); + $date = array(); + + list($future['H'], $future['i'], $future['s'], $future['d'], $future['m'], $future['Y']) = explode('/', date('H/i/s/d/m/Y', $futureTime)); + + list($past['H'], $past['i'], $past['s'], $past['d'], $past['m'], $past['Y']) = explode('/', date('H/i/s/d/m/Y', $pastTime)); + $years = $months = $weeks = $days = $hours = $minutes = $seconds = 0; + + if ($future['Y'] == $past['Y'] && $future['m'] == $past['m']) { + $months = 0; + $years = 0; + } else { + if ($future['Y'] == $past['Y']) { + $months = $future['m'] - $past['m']; + } else { + $years = $future['Y'] - $past['Y']; + $months = $future['m'] + ((12 * $years) - $past['m']); + + if ($months >= 12) { + $years = floor($months / 12); + $months = $months - ($years * 12); + } + + if ($future['m'] < $past['m'] && $future['Y'] - $past['Y'] == 1) { + $years --; + } + } + } + + if ($future['d'] >= $past['d']) { + $days = $future['d'] - $past['d']; + } else { + $daysInPastMonth = date('t', $pastTime); + $daysInFutureMonth = date('t', mktime(0, 0, 0, $future['m'] - 1, 1, $future['Y'])); + + if (!$backwards) { + $days = ($daysInPastMonth - $past['d']) + $future['d']; + } else { + $days = ($daysInFutureMonth - $past['d']) + $future['d']; + } + + if ($future['m'] != $past['m']) { + $months --; + } + } + + if ($months == 0 && $years >= 1 && $diff < ($years * 31536000)) { + $months = 11; + $years --; + } + + if ($months >= 12) { + $years = $years + 1; + $months = $months - 12; + } + + if ($days >= 7) { + $weeks = floor($days / 7); + $days = $days - ($weeks * 7); + } + } else { + $years = $months = $weeks = 0; + $days = floor($diff / 86400); + + $diff = $diff - ($days * 86400); + + $hours = floor($diff / 3600); + $diff = $diff - ($hours * 3600); + + $minutes = floor($diff / 60); + $diff = $diff - ($minutes * 60); + $seconds = $diff; + } + $relativeDate = ''; + $diff = $futureTime - $pastTime; + + if ($diff > abs($now - strtotime($end))) { + $relativeDate = sprintf('on %s', date($format, $inSeconds)); + } else { + if ($years > 0) { + // years and months and days + $relativeDate .= ($relativeDate ? ', ' : '') . $years . ' ' . ($years==1 ? 'año':'años'); + $relativeDate .= $months > 0 ? ($relativeDate ? ', ' : '') . $months . ' ' . ($months==1 ? 'mes':'meses') : ''; + $relativeDate .= $weeks > 0 ? ($relativeDate ? ', ' : '') . $weeks . ' ' . ($weeks==1 ? 'semana':'semanas') : ''; + $relativeDate .= $days > 0 ? ($relativeDate ? ', ' : '') . $days . ' ' . ($days==1 ? 'día':'días') : ''; + } elseif (abs($months) > 0) { + // months, weeks and days + $relativeDate .= ($relativeDate ? ', ' : '') . $months . ' ' . ($months==1 ? 'mes':'meses'); + $relativeDate .= $weeks > 0 ? ($relativeDate ? ', ' : '') . $weeks . ' ' . ($weeks==1 ? 'semana':'semanas') : ''; + $relativeDate .= $days > 0 ? ($relativeDate ? ', ' : '') . $days . ' ' . ($days==1 ? 'día':'días') : ''; + } elseif (abs($weeks) > 0) { + // weeks and days + $relativeDate .= ($relativeDate ? ', ' : '') . $weeks . ' ' . ($weeks==1 ? 'semana':'semanas'); + $relativeDate .= $days > 0 ? ($relativeDate ? ', ' : '') . $days . ' ' . ($days==1 ? 'día':'días') : ''; + } elseif (abs($days) > 0) { + // days and hours + $relativeDate .= ($relativeDate ? ', ' : '') . $days . ' ' . ($days==1 ? 'día':'días'); + $relativeDate .= $hours > 0 ? ($relativeDate ? ', ' : '') . $hours . ' ' . ($hours==1 ? 'hora':'horas') : ''; + } elseif (abs($hours) > 0) { + // hours and minutes + $relativeDate .= ($relativeDate ? ', ' : '') . $hours . ' ' . ($hours==1 ? 'hora':'horas'); + $relativeDate .= $minutes > 0 ? ($relativeDate ? ', ' : '') . $minutes . ' ' . ($minutes==1 ? 'minuto':'minutos') : ''; + } elseif (abs($minutes) > 0) { + // minutes only + $relativeDate .= ($relativeDate ? ', ' : '') . $minutes . ' ' . ($minutes==1 ? 'minuto':'minutos'); + } else { + // seconds only + $relativeDate .= ($relativeDate ? ', ' : '') . $seconds . ' ' . ($seconds==1 ? 'segundo':'segundos'); + } + + if (!$backwards) { + $relativeDate = sprintf('Hace %s', $relativeDate); + } + } + return $relativeDate; + } +} \ No newline at end of file diff --git a/www/protected/views/usuario/index.php b/www/protected/views/usuario/index.php index 13ab0b3..0774911 100644 --- a/www/protected/views/usuario/index.php +++ b/www/protected/views/usuario/index.php @@ -97,7 +97,7 @@ or =) at the beginning of each of your search values to specify how the c array( 'type' => 'raw', 'name' => 'last_login_time', - 'value' => '($data->last_login_time === NULL) ? CHtml::tag("span", array("class"=>"nodata"), "Nunca") : $data->last_login_time;', + 'value' => '($data->last_login_time === NULL) ? CHtml::tag("span", array("class"=>"nodata"), "Nunca") : Time::timeAgoInWords($data->last_login_time);', 'headerHtmlOptions'=>array( 'class' => 'head1', ),