Zum Inhalt springen

Anzahl Tage berechnen

Erstellt am 10. Dezember 2013 · 8 Antworten · letzte Antwort am 11. Dezember 2013

Tags: Fluid, Frage

typo3_hp ·

Hallo!

Ich möchte in meiner Ext. die Anzahl Tage einer Reise berechnen.

Das ganze soll im Template angezeigt werden.

So geht es natürlich nicht:

<div id="duration">
<f:translate key="tx_cpgbusjourneys_domain_model_journeys.duration" />
<p>{journeys.journeyenddate}-{journeys.journeystartdate}&nbsp;</p>
</div>

Ist ein "normales" Typo3-Datum. Hat mir jemand eine Idee/Lösung?

LG und Danke

Schweriner ·

Editiere dein Model und füge für die Berechnung einen getter hinzu, indem die Berechnung stattfindet. Auf den Wert solltest Du dann per Fluid zugreifen können.

typo3_hp ·

Bin glaube ich schon ganz weit gekommen, mit dem ersten eigenen getter ;-)

	/**
	 * Returns the duration of the Journey
	 *
	 * @return integer
	 */
	public function getCaldays($journeyenddate, $journeystartdate) {
		$duration = $journeyenddate - $journeystartdate;
		$duration = $duration / 86400;
		floor($duration);
		return $duration;
	}

Die Variablen $journeyenddate und $journeystartdate werden weiter oben deklariert. Ich kann sie aber irgendwie innerhalb des selben Modells nicht verwenden?

Hier Beispielhaft das startdate:

/**
	 * journeystartdate
	 *
	 * @var \DateTime
	 * @validate NotEmpty
	 */
	protected $journeystartdate;


/**
	 * Returns the journeystartdate
	 *
	 * @return \DateTime $journeystartdate
	 */
	public function getJourneystartdate() {
		return $this->journeystartdate;
	}


/**
	 * Sets the journeystartdate
	 *
	 * @param \DateTime $journeystartdate
	 * @return void
	 */
	public function setJourneystartdate( $journeystartdate ) {
		$this->journeystartdate = $journeystartdate;
	}

Wie kann ich die Variable abgreifen und direkt verwenden?

LG und Danke!

Schweriner ·

Innerhalb der Funktionen musst Du die Variablen mit $this->VARIABLE ansprechen, da Sie sonst als lokale Variablen angesehen werden.

typo3_hp ·

Hi Danke!

Ok, wenn ich nur eine Variable ausgebe

$duration = ($this->journeyenddate)

zeigt er mir zumindest was an.

Wenn ich aber untenaufgeführte Zeilen verwende, kommt immer 0 heraus...!?

Eine Idee?

Danke und LG

	/**
	 * Returns the duration of the Journey
	 *
	 * @return integer
	 */
	public function getCaldays() {
		$duration = ($this->journeyenddate) - ($this->journeystartdate);
		$duration = $duration / 86400;
		floor($duration);
		return $duration;
	}
Schweriner ·

Rufst Du auch ganz bestimmt die richtige Funktion auf? Nimm erstmal alle möglichen Fehlerquellen weg und lass Dir nur mal das Ergebnis der Berechnung zurückgeben:

return $this->journeyenddate-$this->journeystartdate

Und müsste es außerdem nicht sowieso so aussehen?:

$duration = floor($duration);

typo3_hp ·

Hmm.

mit

/**
	 * Returns the duration of the Journey
	 *
	 * @return integer
	 */
	public function getCaldays() {
		//$duration = ($this->journeyenddate) - ($this->journeystartdate);
		//$duration = $duration / 86400;
		//floor($duration);
		//return $duration;
		return $this->journeyenddate-$this->journeystartdate;
	}

bekomme ich auch 0 als Ergebnis :-(

Mit
return 3;

kommt die 3. Also müsste es schon die richtige Funktion sein!

Muss ich diesen Date-Timestamp zuerst noch konvertieren?

var_dump($this->journeyenddate);
var_dump($this->journeystartdate); ergibt ->

object(DateTime)#577 (3) { ["date"]=> string(19) "2013-12-23 00:00:00" ["timezone_type"]=> int(1) ["timezone"]=> string(6) "+01:00" } object(DateTime)#576 (3) { ["date"]=> string(19) "2013-12-23 00:00:00" ["timezone_type"]=> int(1) ["timezone"]=> string(6) "+01:00" } object(DateTime)#600 (3) { ["date"]=> string(19) "2014-02-09 00:00:00" ["timezone_type"]=> int(1) ["timezone"]=> string(6) "+01:00" } object(DateTime)#601 (3) { ["date"]=> string(19) "2014-02-07 00:00:00" ["timezone_type"]=> int(1) ["timezone"]=> string(6) "+01:00" } object(DateTime)#607 (3) { ["date"]=> string(19) "2014-08-10 00:00:00" ["timezone_type"]=> int(1) ["timezone"]=> string(6) "+02:00" } object(DateTime)#612 (3) { ["date"]=> string(19) "2014-08-01 00:00:00" ["timezone_type"]=> int(1) ["timezone"]=> string(6) "+02:00" } object(DateTime)#616 (3) { ["date"]=> string(19) "2014-03-16 00:00:00" ["timezone_type"]=> int(1) ["timezone"]=> string(6) "+01:00" }
...

typo3_hp ·

Vielen Dank!
Habe es nun hinbekommen wie folgt:

*** Hätte das abfangen ob das Ergebnis 0 ist, gerne auch gleich im Model gemacht, aber nicht hinbekommen. Daher im Template ***

Model:

	/**
	 * Returns the duration of the Journey
	 *
	 * @return integer
	 */
	public function getCaldays() {

			$duration = 0;
			$duration = $this->journeyenddate->diff($this->journeystartdate); 			
			($duration->format('%a') == 0) ? 1 : $duration;
			var_dump($duration);
			return $duration->format('%a');
	}

Template:

<f:if condition="{journeys.caldays} == 0">
					<f:then><p>1</p></f:then>
					<f:else><p>{journeys.caldays}</p></f:else>
					</f:if>

Vll. kannst mir ja in der anderen Angelegenheit auch helfen. Siehe PM.

LG