Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP]Przeróbka funkcji operującej na datach
Forum PHP.pl > Forum > Przedszkole
northwest
Witam serdecznie,
Mam następującą funkcję:
  1. $searchtxt = "F|31-10-2014 00:00:00|31-10-2014 07:20:01|usa|Chopin|
  2. D|31-10-2014 10:20:01|31-10-2014 16:20:00|1|2||dzisiaj|FULL||usa|Chopin|
  3. D|31-10-2014 17:20:01|01-11-2014 01:20:00|2|1||jutro|FULL||usa|Chopin|";
  4.  
  5. class Parser {
  6.  
  7. /**
  8.   *
  9.   * @var resource
  10.   */
  11. public $file;
  12.  
  13. /**
  14.   *
  15.   * @param string $file
  16.   */
  17. public function __construct($file) {
  18. $this->file = fopen($file, "r");
  19. }
  20.  
  21. /**
  22.   *
  23.   * @param \DateTime $dateTime
  24.   * @return string|null
  25.   */
  26. public function findConnection(\DateTime $dateTime) {
  27. $timeStamp = $dateTime->format("U");
  28. $resultLine = null;
  29. rewind($this->file);
  30. $minTime = null;
  31. while (!feof($this->file)) {
  32. $line = fgets($this->file);
  33. $time = $this->checkRow($line, $timeStamp);
  34. if($time > 0){
  35. continue;
  36. }
  37. if(is_null($minTime)){
  38. //$minTime = $time;
  39. $resultLine = $line."____PUSTO";
  40. }
  41.  
  42. if($time <= $minTime){
  43. $resultLine = $line;
  44. $minTime = $time;
  45. }
  46. }
  47.  
  48. return $resultLine;
  49. }
  50.  
  51. /**
  52.   *
  53.   * @param string $row
  54.   * @param \DateTime $dateTime
  55.   * @return int
  56.   */
  57. protected function checkRow($row, $dateTime) {
  58. if (empty($row)) {
  59. return -1;
  60. }
  61.  
  62. list(, $a, $b) = explode("|", $row);
  63. try {
  64. $startDate = new \DateTime($a);
  65. $startDate = $startDate->format("U");
  66. $endDate = new \DateTime($b);
  67. $endDate = $endDate->format("U");
  68. } catch (\Exception $e) {
  69. return -1;
  70. }
  71.  
  72. if($dateTime <= $endDate && $dateTime >= $startDate){
  73. return 0;
  74. }
  75.  
  76. if($dateTime > $startDate){
  77. return -1;
  78. }
  79.  
  80. return $startDate - $dateTime;
  81. }
  82.  
  83. /**
  84.   *
  85.   */
  86. public function __destruct() {
  87. fclose($this->file);
  88. }
  89.  
  90. }
  91.  
  92. $szuktime = "31-10-2014 17:40:00";
  93. echo "$szuktime <br/><br/>";
  94. $parser = new Parser("db.txt");
  95. $line = $parser->findConnection(new \DateTime("$szuktime"));
  96.  
  97. var_dump($line);


W zmiennej $searchtxt - znajduje się zawartość pliku "db.txt". Czy mógłby mi ktoś przerobić tą funkcję tak, żeby zamiast operować na danych z pliku operacje odbywałyby się w danych ze zmiennej: $searchtxt?

Programuję strukturalnie i nie potrafię tego zmienić sad.gif

Bardzo proszę o pomoc,
Northwest
Turson
  1. public function __construct($file) {
  2. $this->file =$file;
  3. }
Pyton_000
Turson dalej masz czytanie z pliku smile.gif
Turson
Aaa dalej nawet nie czytałem wink.gif
northwest
Dokladnie, czyta z pliku - a potrzebuje to zmienic na czytanie ze zmiennej smile.gif
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.