[PHP] Berechnungsproblem

COOLover

Programmierer
ID: 66135
L
25 April 2006
191
10
Hallo zusammen,

ich soll in der Schule ein kleines Script schreiben.

So eine Art Verpflegungskosten Berechnung für X Personen (Lieferservice o.ä.)
Das Script habe ich soweit auch fertig und funktioniert auch.

Jedoch weiß ich nicht, wie ich folgendes mache:

1. Quatal = 1€ , 2. Quatal = 2€ , 3. Quatal = 3€ , 4. Quatal = 4€

Wenn ich nun also vom 20.03. - 03.04. was "Bestelle" sind 3 Tage im 2. Quatal.
Also 10x1€+3x2€

Wie bekomme ich das mit PHP hin das er dies teilt?

Derzeit habe ich das nur mit dem Anfangsmonat (vom Beispiel März also 13x1€)
PHP:
		if($time1a[1] == '1' OR $time1a[1] == '2' OR $time1a[1] == '3') {
			$euro=$price1 * $days * $peoples;
		}
		elseif($time1a[1] == '4' OR $time1a[1] == '5' OR $time1a[1] == '6') {
			$euro=$price2 * $days * $peoples;
		}
		elseif($time1a[1] == '7' OR $time1a[1] == '8' OR $time1a[1] == '9') {
			$euro=$price3 * $days * $peoples;
		}
		elseif($time1a[1] == '10' OR $time1a[1] == '11' OR $time1a[1] == '12') {
			$euro=$price4 * $days * $peoples;
		}

Wäre dankbar über eure Hilfe.
 
So vieleicht ?

PHP:
$euro=0;
if($time1a[1] == '1' OR $time1a[1] == '2' OR $time1a[1] == '3') { 
            $euro=$euro+$price1 * $days * $peoples; 
        } 
if($time1a[1] == '4' OR $time1a[1] == '5' OR $time1a[1] == '6') { 
            $euro=$euro+$price2 * $days * $peoples; 
        } 
if($time1a[1] == '7' OR $time1a[1] == '8' OR $time1a[1] == '9') { 
            $euro=$euro+$price3 * $days * $peoples; 
        } 
if($time1a[1] == '10' OR $time1a[1] == '11' OR $time1a[1] == '12') { 
            $euro=$euro+$price4 * $days * $peoples; 
        }

Gruss
Der Pit
 
Raten bringt wohl nix :roll:
Du kannst schlecht bis zu 4x $days draufaddieren.

Es wäre erstmal sinnvoll, zu wissen, welche Variable für was steht. Dann sieht man nämlich auch, was das Problem is.
Die Lösung steht ja eigentlich schon in Post #1:
Also 10x1€+3x2€
Man muss die beiden (mehreren) Intervalle aufteilen und einzeln aufaddieren.
 
Ich würde die Monate in ner eigenen Variable Speichern also Montag (Ende) - Monat (Anfang). Und dann kannst du ja schauen ob der Montag(anfang im ersten Quartal liegt) also kleiner gleich 3 ist und dann halt schauen ob wie groß die Differenz ist und dann falls größer 3 eben nochmal aufteilen....
 
Hi, erst einmal danke für eure hilfe

@quasimodro
du hast einfach die else entfernt das geht natürlich auch - aber behandelt nicht mein Anliegen ;)


Die Variablen:
PHP:
$time1="20.03.2009";
$time2="03.04.2009";
$peoples=1;
$days=round((mktime(0,0,0,$time2a[1],$time2a[0],$time2a[2])-mktime(0,0,0,$time1a[1],$time1a[0],$time1a[2]))/24/60/60);
$pricea[1]="1.00";	$pricea[2]="2.00";	$pricea[3]="3.00";	$pricea[4]="4.00";
$price1=$pricea[1]; $price2=$pricea[2]; $price3=$pricea[3]; $price4=$pricea[4];


	$time1a=explode(".", $time1);
	$time2a=explode(".", $time2);

Hatte zwar irgendwie an timestamp gedacht aber wie genau bin ich ein wenig überfragt.

Demo: https://www.marcof.de/coding/schule/
Script: https://www.marcof.de/coding/schule/berechnung.rar
 
Naja, das einfachste wäre doch, die beiden Daten jeweils in Timestamps umzuwandeln und dann vom Anfangs-Timestamp bis zum End-Timestamp Tag für Tag durchzugehen, gucken, in welchem Quartal dieser Timestamp liegt und dementsprechend die Gesamtsumme aufzubauen.

PHP:
$prices = array(0=>1, 1=>2, 2=>3, 3=>4);
$timestamp_a = mktime(0,0,0,3,20,2009);
$timestamp_b = mktime(0,0,0,4,3,2009);

$price = 0;
while ($timestamp_a <= $timestamp_b)
{
  $price += $prices[ floor((date('n')-1)/3) ];
  $timestamp_a += 24*60*60;
  // Alternativ: $timestamp_a = strtotime('tomorrow', $timestamp_a);
}

Sicherlich kann man da dann noch optimieren (von Anfang berechnen, wieviele Tage in welchem Quartal liegen), aber fürs erste sollte es so funktionieren.
 
Zuletzt bearbeitet:
:kiss::kiss::kiss::kiss:

danke jetzt klappt es.
Hing echt auf dem schlauch.... DANKE

PHP:
		$time1a=explode(".", $time1);
		$time2a=explode(".", $time2);

		$timestamp_a = mktime(0,0,0,$time1a[1],$time1a[0],$time1a[2]);
		$timestamp_b = mktime(0,0,0,$time2a[1],$time2a[0],$time2a[2]);

		$euro=0.00;

		while ($timestamp_a <= $timestamp_b)
		{
			$datumm = date("n", $timestamp_a);

			if($datumm == '1' OR $datumm == '2' OR $datumm == '3') {
				$euro+=$price1 * $peoples;
			}
			elseif($datumm == '4' OR $datumm == '5' OR $datumm == '6') {
				$euro+=$price2 * $peoples;
			}
			elseif($datumm == '7' OR $datumm == '8' OR $datumm == '9') {
				$euro+=$price3 * $peoples;
			}
			elseif($datumm == '10' OR $datumm == '11' OR $datumm == '12') {
				$euro+=$price4 * $peoples;
			}else{
				$euro+=0.00;
			}

			$timestamp_a += 24*60*60; 
		}