Parcourir la source

PHP : performances fonction isDay

Amélioration des performances de Entry->isDay()
Alexandre Alapetite il y a 12 ans
Parent
commit
847de9b329

+ 1 - 0
app/Controllers/indexController.php

@@ -125,6 +125,7 @@ class FreshRSS_index_Controller extends Minz_ActionController {
 		}
 
 		$today = @strtotime('today');
+		$this->view->today = $today;
 
 		// on calcule la date des articles les plus anciens qu'on affiche
 		$nb_month_old = $this->view->conf->oldEntries ();

+ 13 - 14
app/Models/Entry.php

@@ -134,21 +134,20 @@ class FreshRSS_Entry extends Minz_Model {
 		$this->tags = $value;
 	}
 
-	public function isDay ($day) {
+	public function isDay ($day, $today) {
 		$date = $this->dateAdded(true);
-		$today = @strtotime('today');
-		$yesterday = $today - 86400;
-
-		if ($day === FreshRSS_Days::TODAY &&
-		    $date >= $today && $date < $today + 86400) {
-			return true;
-		} elseif ($day === FreshRSS_Days::YESTERDAY &&
-		    $date >= $yesterday && $date < $yesterday + 86400) {
-			return true;
-		} elseif ($day === FreshRSS_Days::BEFORE_YESTERDAY && $date < $yesterday) {
-			return true;
-		} else {
-			return false;
+		switch ($day) {
+			case FreshRSS_Days::TODAY:
+				$tomorrow = $today + 86400;
+				return $date >= $today && $date < $tomorrow;
+			case FreshRSS_Days::YESTERDAY:
+				$yesterday = $today - 86400;
+				return $date >= $yesterday && $date < $today;
+			case FreshRSS_Days::BEFORE_YESTERDAY:
+				$yesterday = $today - 86400;
+				return $date < $yesterday;
+			default:
+				return false;
 		}
 	}
 

+ 1 - 1
app/layout/nav_menu.phtml

@@ -72,7 +72,7 @@
 				<li class="item"><a href="<?php echo $markReadUrl; ?>"><?php echo $string_mark; ?></a></li> 
 				<li class="separator"></li>
 <?php
-	$today = @strtotime('today');
+	$today = $this->today;
 	$one_week = $today - 604800;
 ?>
 				<li class="item"><a href="<?php echo _url ('entry', 'read', 'is_read', 1, 'get', $get, 'idMax', $today . '000000'); ?>"><?php echo Minz_Translate::t ('before_one_day'); ?></a></li>

+ 4 - 3
app/views/helpers/view/normal_view.phtml

@@ -21,24 +21,25 @@ if (!empty($this->entries)) {
 		$facebook = $this->conf->sharing ('facebook');
 		$email = $this->conf->sharing ('email');
 		$print = $this->conf->sharing ('print');
+		$today = $this->today;
 	?>
 	<?php foreach ($this->entries as $item) { ?>
 
-	<?php if ($display_today && $item->isDay (FreshRSS_Days::TODAY)) { ?>
+	<?php if ($display_today && $item->isDay (FreshRSS_Days::TODAY, $today)) { ?>
 	<div class="day" id="day_today">
 		<?php echo Minz_Translate::t ('today'); ?>
 		<span class="date"> - <?php echo timestamptodate (time (), false); ?></span>
 		<span class="name"><?php echo $this->currentName; ?></span>
 	</div>
 	<?php $display_today = false; } ?>
-	<?php if ($display_yesterday && $item->isDay (FreshRSS_Days::YESTERDAY)) { ?>
+	<?php if ($display_yesterday && $item->isDay (FreshRSS_Days::YESTERDAY, $today)) { ?>
 	<div class="day" id="day_yesterday">
 		<?php echo Minz_Translate::t ('yesterday'); ?>
 		<span class="date"> - <?php echo timestamptodate (time () - 86400, false); ?></span>
 		<span class="name"><?php echo $this->currentName; ?></span>
 	</div>
 	<?php $display_yesterday = false; } ?>
-	<?php if ($display_others && $item->isDay (FreshRSS_Days::BEFORE_YESTERDAY)) { ?>
+	<?php if ($display_others && $item->isDay (FreshRSS_Days::BEFORE_YESTERDAY, $today)) { ?>
 	<div class="day" id="day_before_yesterday">
 		<?php echo Minz_Translate::t ('before_yesterday'); ?>
 		<span class="name"><?php echo $this->currentName; ?></span>