| Autor | Zpráva | ||
|---|---|---|---|
| michaelaw Profil * |
Ahoj, tvořím do CMS Joomla plugin, který bude vracet termíny nějakých událostí konaných v určitých městech. V části s výpočtem musím mít nějakou, nejspíš dost hloupou chybu, ale protože už do toho koukám vážně dlouho, nevidím jí.
Zadání od uživatele: Začátek (start) akce v prvním městě - datum Města: názvy měst konání odd. čárkami, může jich být nekonečně mnoho Rozdíl: počet týdnů - rozdíl v termínech mezi jedn. městy - tj. máme např. města Praha, Plzeň, na Peahu připadá termín "start", Plzeň = start + rozdíl Perioda: až akce proběhne postupně ve všech městech, perioda (v týdnech) určuje, za jak dlouho se bude opakovat v počátečním městě, a následně ve všech dalších. Tj. např. perioda = 20, v Praze se bude akce konat opět 20 týdnů po "startu". Doba trvání - určená jako čísla dnů v týdnu - např. 1,2,4 - několikadenní akce (např. kurz) bude v pondělí (start), úterý a čtvrtek. Počet vrácených termínů - kolik cyklů chci vypsat (kolikrát přičítat periodu). a výstup asi ve stylu: Praha: 3.9.2012, 4.9.2012, 6.9.2012 12.11.2012, 13.11.2012, 15.11.2012 Plzeň: 10.9.2012, 11.9.2012, 13.9.2012 19.11.2012, 20.11.2012, 22.11.2012 = rozdíl: 1 týden = perioda: 10 týd. = počet: 2 = start: 3.9.2012 . . . Je možné vypsat pouze termíny konané v budoucnu. Pokud se zadá datum v minulosti, posune se termín o týden. V čem je problém: někde v kódu mám chybu, takže výsl. pole vypadá takhle: http://walterwebs.eu/ (špatně vypisuje termíny vypočítané pomocí hodnoty "rozdílu") + bude dále ještě potřeba porovnat pole s db, kde jsou uloženy svátky a pokud termín vyjde na svátek, posunout ho o týden. testovací kód: (snad nevadí anglické komentáře) <?php
// some values for variables
$start = "03.09.2012";
$periode = "10";
$differenz = "1";
$dauer = "1,3,5";
$anzahl = "3";
$orte = "Horb, Stuttgart, Basel, Karlsruhe";
$wochearray = explode(",","$dauer"); // get the weekdays numbers
$ortearray = explode(",","$orte"); // get an array from "orte" input
$ortcount = count($ortearray); // count the towns in array
$daycount = count($wochearray); //the course length in days
$s = strtotime($start); //getting the correct format of "start" input
$from_now = time()-$s; //allow only the dates in future
if ($from_now>0) {
do {
$s = $s+650000;
} while ((time()-$s)>0);
}
$star = StrFTime("%d.%m.%Y", $s); //human readable format
function tag($date) { // find the weekday of "start" day
$position = StrPos ($date,"(");
$tag= substr($date, $position+1,2);
return $tag;
}
function tagnom ($tag) { // transfer the name of day to number - beginning with 1 - "0" is not ideal for counting
if ($tag == "Mo") {
$tagnr = 1;
} elseif($tag == "Di") {
$tagnr = 2;
} elseif($tag == "Mi") {
$tagnr = 3;
} elseif($tag == "Do") {
$tagnr = 4;
} elseif($tag == "Fr") {
$tagnr = 5;
} elseif($tag == "Sa") {
$tagnr = 6;
} elseif($tag == "So") {
$tagnr = 7;
}
return $tagnr;
}
//FUNCTION FOR EXCLUDING VACATIONS - STILL NEED TO BE TUNED
/* function istfeiertag($date) {
$id_connection = mysql_connect("****","******","*****");
mysql_select_db ("d27523_joomla1", $id_connection);
$sta = strtotime("$date");
$year = StrFTime("%Y", $sta);
$varfeier = mysql_query ("SELECT * FROM jos_feiertage WHERE A='$year'",$id_connection);
while ($feier1 = mysql_fetch_array($varfeier)) {
if (in_array($date, $feier1)) {
do {
$newdate1 = strtotime("$date+1 week");
$newdate = StrFTime("%d.%m.%Y", $newdate1);
} while (in_array($date, $feier1));
} else {
//do nothing
$newdate = $date;
}
return $newdate;
}
} */
function get_start_dates ($beg, $diff, $wochearray) { //count the array of starting dates
$sta = strtotime("$beg+$diff days");
$star = StrFTime("%d.%m.%Y", $sta);
SetLocale(LC_ALL, 'de_DE');
$stat = StrFTime("%d.%m.%Y (%a)", $sta);
$stat2 = StrFTime("%d.%m.%Y", $sta);
$tg = tagnom(tag($stat));
foreach ($wochearray as $w3) { // counting the dates following immadiately after start (based on weekdays from input)
$zk3 = $w3-$tg;
$neco3 = strtotime("$stat2+$zk3 days");
$terminyc[] = StrFTime("%d.%m.%Y", $neco3);
}
/*foreach ($terminyc as $term) {
$terminyc[] = istfeiertag($term);
} */ // INCLUDE ISTFEIERTAG() FUNCTION AFTER IT WORKS
return $terminyc; // return array
}
function plustime($term, $periode) { //term + "periode" weeks
//$term = istfeiertag($term); - include after it works
$temp = strtotime("$term+$periode weeks");
SetLocale(LC_ALL, 'de_DE');
$result = StrFTime("%d.%m.%Y", $temp);
return $result;
}
$barray = array(array(array()));
/*WE WILL NOT PROBABLY USE THIS
function countanother($newa, $per, $an, $dau, $n) { //count another dates form strart - based on plustime()
$z = 0;
$plus = $per;
//$helpingstring = implode(";",$barray[$n][1]);
//$newa = current($barray[$n][1]);
//$newa = explode(";", "$helpingstring");
foreach ($newa as $b) {
while ($z < $an-1) {
$counting_browsing = 0;
$abteil_j = ""; // variable - break
$abt_abt = ""; // variable to separate (",")
$counting_browsing = $counting_browsing +1 ; //counting foreach
if ($counting_browsing < $dau) $abt_abt = ", "; //where to add ","
if ($counting_browsing == $dau) $abteil_j = "<br />"; //where to add break
$periode1[] = plustime($b, $plus) . $abt_abt . $abteil_j; //all together
$z++;
$plus = $plus+$per;
}
}
return $periode1;
}
*/
$diffe = $differenz*7; //differenz in days
$diff = 0; //we start with 0 - nothing to add to 1st date (start)
$n = 0;
$an = $anzahl;
//barray - our main array
while ($n < $ortcount) { //get start terms for all the cities
foreach ($ortearray as $o) {
$helpingstring = join(";",get_start_dates($star, $diff, $wochearray));
$newa = explode(";", $helpingstring);
$z = 0;
$plus = $periode;
while ($z < $an) {
foreach ($newa as $b) {
$periode1[$z] = plustime($b, $plus);
$z++;
$plus = $plus+$periode;
}
}
$barray[$n] = array(0=>$o, 1=>get_start_dates($star, $diff, $wochearray), 2=>$periode1);
$diff += $diffe; // increasing the difference(from start) after every browse
$n++;
}
}
//testing printing
echo '<pre>';
print_r($barray);
?>--- |
||
|
Časová prodleva: 20 dní
|
|||
| peta Profil |
Opravil jsem tam nektere chyby. Smaz si pak vsechno, kde je komnetar //pp nebo echo. Pripadne si precti dopsany komentar u pp. A trochu jsem ti to prehazel, kod k sobe, funkce zvlast.
<?php
function tag($date) { // find the weekday of "start" day
$position = StrPos ($date,"(");
$tag= substr($date, $position+1,2);
return $tag;
}
function tagnom ($tag) { // transfer the name of day to number - beginning with 1 - "0" is not ideal for counting
echo $tag; //pp
if ($tag == "Mo") {
$tagnr = 1;
} elseif($tag == "Di") {
$tagnr = 2;
} elseif($tag == "Mi") {
$tagnr = 3;
} elseif($tag == "Do") {
$tagnr = 4;
} elseif($tag == "Fr") {
$tagnr = 5;
} elseif($tag == "Sa") {
$tagnr = 6;
} elseif($tag == "So") {
$tagnr = 7;
}
return $tagnr;
}
//FUNCTION FOR EXCLUDING VACATIONS - STILL NEED TO BE TUNED
/* function istfeiertag($date) {
$id_connection = mysql_connect("****","******","*****");
mysql_select_db ("d27523_joomla1", $id_connection);
$sta = strtotime("$date");
$year = StrFTime("%Y", $sta);
$varfeier = mysql_query ("SELECT * FROM jos_feiertage WHERE A='$year'",$id_connection);
while ($feier1 = mysql_fetch_array($varfeier)) {
if (in_array($date, $feier1)) {
do {
$newdate1 = strtotime("$date+1 week");
$newdate = StrFTime("%d.%m.%Y", $newdate1);
} while (in_array($date, $feier1));
} else {
//do nothing
$newdate = $date;
}
return $newdate;
}
} */
function get_start_dates ($beg, $diff, $wochearray)
{ //count the array of starting dates
$sta = strtotime("$beg+$diff days");
echo strtotime($beg)."<br>";
echo $sta."<br>";
echo "$beg+$diff days<br>"; //pp
$star = strftime("%d.%m.%Y", $sta);
echo $star."<br>";
//(LC_ALL, 'de_DE'); //pp
//setlocale(LC_ALL, 'de_DE@euro', 'de_DE', 'de', 'ge'); //pp
setlocale(LC_ALL, "de_DE", "de_DE@euro", "deu", "deu_deu", "german"); //pp protoze echo $stat dole zobrazovalo nazev v anglictine
$stat = strftime("%d.%m.%Y (%a)", $sta);
echo $stat."<br>";
$stat2 = strftime("%d.%m.%Y", $sta);
$tg = tagnom(tag($stat)); // pp nesmysl, echo $stat = 25.09.2012 (Tue)
// pp navic, den v tydnu se zjistuje pres date('N',$time) http://php.net/manual/en/function.date.php
//$x = (string) strftime("%a", $sta);
//$tg = tagnom(tag($x));
foreach ($wochearray as $w3) { // counting the dates following immadiately after start (based on weekdays from input)
$zk3 = $w3-$tg;
$neco3 = strtotime("$stat2+$zk3 days");
$terminyc[] = StrFTime("%d.%m.%Y", $neco3);
}
/*foreach ($terminyc as $term) {
$terminyc[] = istfeiertag($term);
} */ // INCLUDE ISTFEIERTAG() FUNCTION AFTER IT WORKS
return $terminyc; // return array
}
function plustime($term, $periode) { //term + "periode" weeks
//$term = istfeiertag($term); - include after it works
$temp = strtotime("$term+$periode weeks");
SetLocale(LC_ALL, 'de_DE');
$result = StrFTime("%d.%m.%Y", $temp);
return $result;
}
/*WE WILL NOT PROBABLY USE THIS
function countanother($newa, $per, $an, $dau, $n) { //count another dates form strart - based on plustime()
$z = 0;
$plus = $per;
//$helpingstring = implode(";",$barray[$n][1]);
//$newa = current($barray[$n][1]);
//$newa = explode(";", "$helpingstring");
foreach ($newa as $b) {
while ($z < $an-1) {
$counting_browsing = 0;
$abteil_j = ""; // variable - break
$abt_abt = ""; // variable to separate (",")
$counting_browsing = $counting_browsing +1 ; //counting foreach
if ($counting_browsing < $dau) $abt_abt = ", "; //where to add ","
if ($counting_browsing == $dau) $abteil_j = "<br />"; //where to add break
$periode1[] = plustime($b, $plus) . $abt_abt . $abteil_j; //all together
$z++;
$plus = $plus+$per;
}
}
return $periode1;
}
*/
//---CODE---
// some values for variables
$start = "03.09.2012"; //pp dd.mm.yyyy ?
$periode = "10";
$differenz = "1";
$dauer = "1,3,5";
$anzahl = "3";
$orte = "Horb, Stuttgart, Basel, Karlsruhe";
$wochearray = explode(",","$dauer"); // get the weekdays numbers
$ortearray = explode(",","$orte"); // get an array from "orte" input
$ortcount = count($ortearray); // count the towns in array
$daycount = count($wochearray); //the course length in days
list($d,$m,$y) = explode(".",$start); //pp
echo "$y-$m-$d<br>"; //pp
$s = strtotime("$y-$m-$d"); //getting the correct format of "start" input //pp YYYY-MM-DD
echo $start.'|'.date("d.m.Y",$s)."<br>"; //pp test, zda se oba datumy shoduji
$from_now = time()-$s; //allow only the dates in future
if ($from_now>0) {
do {
$s = $s+650000; // pp - co znamena cislo 650.000 ?
} while ((time()-$s)>0);
}
$star = StrFTime("%d.%m.%Y", $s); //human readable format
echo $star."<br>";
$barray = array(array(array()));
$diffe = $differenz*7; //differenz in days
$diff = 0; //we start with 0 - nothing to add to 1st date (start)
$n = 0;
$an = $anzahl;
//barray - our main array
while ($n < $ortcount) { //get start terms for all the cities
foreach ($ortearray as $o)
{
$helpingstring = join(";",get_start_dates($star, $diff, $wochearray));
$newa = explode(";", $helpingstring);
$z = 0;
$plus = $periode;
while ($z < $an)
{
foreach ($newa as $b)
{
$periode1[$z] = plustime($b, $plus);
$z++;
$plus = $plus+$per; //pp neni definovana promenna $per a nemam zdani, co tam dat
}
}
$barray[$n] = array(0=>$o, 1=>get_start_dates($star, $diff, $wochearray), 2=>$periode1);
$diff += $diffe; // increasing the difference(from start) after every browse
$n++;
}
}
//testing printing
echo '<pre>';
print_r($barray);
echo '
a vystup asi ve stylu:
Praha:
3.9.2012, 4.9.2012, 6.9.2012
12.11.2012, 13.11.2012, 15.11.2012
Plzen:
10.9.2012, 11.9.2012, 13.9.2012
19.11.2012, 20.11.2012, 22.11.2012
= rozdil: 1 tĹtden
= perioda: 10 tĹtd.
= pocet: 2
= start: 3.9.2012
'; |
||
|
Časová prodleva: 14 let
|
|||
0