Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: PRoblem z funkcją formatującą datę
Forum PHP.pl > Forum > PHP
dominick
Witam temat już był wałkowany jednak proszę o pomoc.

mam funkcje, która:
-pobiera czas i zwraca relatywny czyli ile mineło od tego czasu. Funkcja działa ale jeżeli ktoś doda coś o 23:00 to przez 24 godziny wyświetla mu się "dzisiaj". Chciałbym aby wyświetlało się już wczoraj a po 2 dniach wyświetlała się data w formacie d-m w tym, że miesiąc po polsku a nie liczba.

Czy ktoś wie jak przerobić istniejącą już i działającą funkcje?
funkcja dla czasu relatywnego:

  1. public static function relative($time, $dateformat = 'd.m.Y, H:i')
  2. {
  3. $peroid = array('minute' => 60, 'hour' => 3600, 'day' => 86400, 'week' => 604800);
  4. $time = time()-strtotime($time);
  5.  
  6.  
  7.  
  8. if(($time/$peroid['hour']) < 24)
  9. {
  10.  
  11.  
  12. if(round($time/$peroid['hour']) >= 0 and round($time/$peroid['hour']) <= 24)
  13. return 'dzisiaj';
  14.  
  15. return 'dzisiaj';
  16. }
  17.  
  18. elseif(($time/$peroid['day']) < 7)
  19. {
  20. if(floor(($time/$peroid['day'])) == 1)
  21. return 'wczoraj';
  22.  
  23. if(floor(($time/$peroid['day'])) == 2)
  24. return 'przedwczoraj';
  25.  
  26. return floor(($time/$peroid['day'])) . ' dni temu';
  27. }
  28.  
  29. elseif(($time/$peroid['week']) < 4)
  30. {
  31. if(floor(($time/$peroid['week'])) == 1)
  32. return 'tydzień temu';
  33.  
  34. return floor(($time/$peroid['week'])) . ' tygodnie temu';
  35. }
  36. else
  37. {
  38. return date($dateformat, time()+$time);
  39. }
  40. }


sposób wyświetlania czasu
  1. <?php echo date::relative($a->annoucement_date_added, 'announcement') ?>

Greg0
A nie lepiej po prostu porównywać daty?
Najlepiej by było uzyć do tego klasy DateTime
  1. $relative = new Datetime('2014-09-15');
  2.  
  3. $current = new Datetime('today');
  4. if($current == $relative) echo 'dzisiaj';
  5.  
  6. $current->modify('-1 day');
  7. if($current == $relative) echo 'wczoraj';
  8.  
  9. $current->modify('-1 day');
  10. if($current == $relative) echo 'przedwczoraj';
  11.  
  12. elseif($current > $relative) echo $relative->format('d F Y');
dominick
Hmm tylko nie mogę sobie poradzić z wyświetleniem tej daty. Jak widać na początku wyświetlana jest za pomocą funkcji, której przesłałem. 3 dni już nie śpie i staram się do tego dojść...
kalipek
  1. public static function relative($userDTString, $format = 'd.m.Y, H:i', $relativeTo = 'now')
  2. {
  3. $dateTime = new DateTime($relativeTo);
  4. $dateTime->setTime(0,0,0);
  5.  
  6. $userDT = new DateTime($userDTString);
  7. $userDT->setTime(0,0,0);
  8.  
  9. if($userDT == $dateTime)
  10. return 'dzisiaj';
  11. elseif($userDT == $dateTime->modify('- 1 day'))
  12. return 'wczoraj';
  13. elseif($userDT == $dateTime->modify('- 1 day'))
  14. return 'przedwczoraj';
  15.  
  16. for ($i=3; $i < 7; $i++) {
  17. if($userDT == $dateTime->modify('- 1 day'))
  18. return $i.' dni temu';
  19. }
  20.  
  21. if($userDT == $dateTime->modify('- 1 day'))
  22. return 'tydzień temu';
  23. if($userDT == $dateTime->modify('- 2 weeks'))
  24. return '3 tygodnie temu';
  25. if($userDT > $dateTime && $userDT < $dateTime->modify('+ 1 week'))
  26. return 'ponad 2 tygodnie temu';
  27. if($userDT == $dateTime)
  28. return '2 tygodnie temu';
  29. if($userDT > $dateTime && $userDT < $dateTime->modify('+ 1 week'))
  30. return 'ponad tydzień temu';
  31.  
  32. $older = new DateTime($userDTString);
  33. return $older->format($format);
  34. }
  35.  
  36.  
  37. echo date::relative('1.09.2014, 22:20');
dominick
Super działa. Jest jeden mały szczegół jak już wyświetla datę starszą to wykrzacza takie coś:
pm99201400000092014-09-13T19:29:12+02:00Europe/Warsaw09Europe/Warsaw930
Rozumiem, że coś z formatowaniem teraz muszę dojść co jest nie tak skoro format ustawiony był na początku funkcji.

----
rozwiązałem wystarczyło jeszcze raz do zmiennej format przypisać jaki format ma się wyswietlić.
a dokładnie:
$format = 'd.mY, H:i';
---
Pozostaje problem jak do tej funkcji aby już niemieszać zmienić formatowanie daty z polską nazwą miesiąca Wrz zamiast Sep.

mimo wszystko dziękuje za pomoc wciskam pomógł ; )
ZaXaZ
Cytat(dominick @ 16.09.2014, 11:45:18 ) *
Pozostaje problem jak do tej funkcji aby już niemieszać zmienić formatowanie daty z polską nazwą miesiąca Wrz zamiast Sep.


  1. $pl_czas = str_replace(array('Sep', 'OtherMonth'), array('Wrz', 'InnyMiesiac'), $czas);
dominick
Dzękuje za zainteresowanie.
Nie uzupełniałem całej gdyż chciałem sprawdzić czy działa i niestety nie. (Chyba, że zły czas podstawiłem)
  1. public static function relative($userDTString, $format = 'j.M.Y, H:i', $relativeTo = 'now')
  2. {
  3. $arrLocales = array('pl_PL', 'pl','Polish_Poland.28592');
  4. setlocale( LC_ALL, $arrLocales );
  5. $dateTime = new DateTime($relativeTo);
  6. $dateTime->setTime(0,0,0);
  7.  
  8. $userDT = new DateTime($userDTString);
  9. $userDT->setTime(0,0,0);
  10.  
  11. if($userDT == $dateTime)
  12. return 'dzisiaj';
  13. elseif($userDT == $dateTime->modify('- 1 day'))
  14. return 'wczoraj';
  15. elseif($userDT == $dateTime->modify('- 1 day'))
  16. return '2 dni temu';
  17.  
  18.  
  19.  
  20. $format='j M';
  21. $userDT = str_replace(array('Sep', 'OtherMonth'), array('Wrz', 'InnyMiesiac'), $format);
  22.  
  23.  
  24. $older = new DateTime($userDTString);
  25.  
  26. return $older->format($format);
  27. }


Moje logiczne myślenie jeszcze śpi - ostatnia modyfikacja - dalej nie działa
kalipek
Jeśli masz rozszerzenie intl, możesz użyć:

  1. public static function relative($userDTString, $format = 'd.m.Y, H:i', $relativeTo = 'now')
  2. {
  3. // ...
  4.  
  5. $formatter = new IntlDateFormatter('pl_PL', IntlDateFormatter::MEDIUM, IntlDateFormatter::MEDIUM);
  6. $formatter->setPattern($format);
  7.  
  8. $older = new DateTime($userDTString);
  9. return $formatter->format($older);
  10. }
  11.  
  12. echo date::relative('1.05.2014, 22:20', 'd LLLL Y');
  13.  


odnośnie formatu, tutaj masz tabelę, w której możesz podejrzeć jakie są opcje http://userguide.icu-project.org/formatparse/datetime


Jeśli nie masz intl, możesz po prostu rozszerzyć DateTime i dopisać co potrzebujesz.
dominick
Intl nie działa czy możesz mnie bardziej naprowadzić na rozszerzenie DateTime?
kalipek
  1. class StupidDateTime extends DateTime {
  2.  
  3. public function __construct($time='now', $timezone='Europe/Warsaw')
  4. {
  5. parent::__construct($time, new DateTimeZone($timezone));
  6. }
  7.  
  8. public function format($format)
  9. {
  10. return $this->enToPl(parent::format($format));
  11. }
  12.  
  13. private function enToPl($format)
  14. {
  15. $en = ['January','February','March','April','May'];
  16. $en2 = ['Jan','Feb','Mar','Apr','May'];
  17. $pl = ['Stycznia','Lutego','Marca','Kwietnia','Maja'];
  18. $pl2 = ['Sty','Lut','Mar','Kwi','Maj'];
  19. return str_replace($en2, $pl2, str_replace($en, $pl, $format));
  20. }
  21. }


dopisz pozostałe miesiące, dodaj do autoloadera czy jakkolwiek dodajesz klasy, później w funkcji relative zamień DateTime na StupidDateTime
dominick
Coś pokręciłem? bo mi błąd wywaliło
  1. class StupidDateTime extends DateTime {
  2.  
  3. public function __construct($time='now', $timezone='Europe/Warsaw')
  4. {
  5. parent::__construct($time, new DateTimeZone($timezone));
  6. }
  7.  
  8. public function format($format)
  9. {
  10. return $this->enToPl(parent::format($format));
  11. }
  12.  
  13. private function enToPl($format)
  14. {
  15. $en = ['January','February','March','April','May','June','July','August','September','October','November','December'];
  16. $en2 = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
  17. $pl = ['Stycznia','Lutego','Marca','Kwietnia','Maja','Czerwca','Lipcac','Sierpnia','Września','Października','Listopada','Grudnia'];
  18. $pl2 = ['Sty','Lut','Mar','Kwi','Maj','Cze','Lip','Sie','Wrz','Paź','Lis','Gru'];
  19. return str_replace($en2, $pl2, str_replace($en, $pl, $format));
  20. }
  21. }
  22. class Date extends I18n_Date {
  23. function strftimeV($format,$timestamp){
  24. return iconv("ISO-8859-2","UTF-8",ucfirst(strftime($format,$timestamp)));
  25. }
  26.  
  27.  
  28.  
  29. public static function relative($userDTString, $format = 'j.M.Y, H:i', $relativeTo = 'now')
  30. {
  31.  
  32. $StupidDateTime = new DateTime($relativeTo);
  33. $StupidDateTime->setTime(0,0,0);
  34.  
  35. $userDT = new DateTime($userDTString);
  36. $userDT->setTime(0,0,0);
  37.  
  38. if($userDT == $dateTime)
  39. return 'dzisiaj';
  40. elseif($userDT == $StupidDateTime->modify('- 1 day'))
  41. return 'wczoraj';
  42. elseif($userDT == $StupidDateTime->modify('- 1 day'))
  43. return '2 dni temu';
  44.  
  45.  
  46.  
  47. $format='j M';
  48.  
  49.  
  50.  
  51. $older = new DateTime($userDTString);
  52.  
  53. return $older->format($format);
  54. }
  55.  
  56.  
  57.  
  58.  
  59. public static function my($value, $type = NULL)
  60. {
  61. $format = NULL;
  62.  
  63. if ($type !== NULL)
  64. {
  65. $format = Kohana::$config->load('date.' . $type);
  66. }
  67.  
  68. if ($format === NULL)
  69. {
  70. $format = 'd-m-Y';
  71.  
  72. }
  73.  
  74. if (is_numeric($value))
  75. {
  76. $date = date($format, $value);
  77. }
  78. else
  79. {
  80. $date = strtotime($value);
  81. $date = date($format, $date);
  82. }
  83. return $date;
  84. }
  85. public static function myy($value, $type = NULL)
  86. {
  87. $format = NULL;
  88.  
  89. if ($type !== NULL)
  90. {
  91. $format = Kohana::$config->load('date.' . $type);
  92. }
  93.  
  94. if ($format === NULL)
  95. {
  96. $format = 'H:i';
  97.  
  98. }
  99.  
  100. if (is_numeric($value))
  101. {
  102. $date = date($format, $value);
  103. }
  104. else
  105. {
  106. $date = strtotime($value);
  107. $date = date($format, $date);
  108. }
  109. return $date;
  110. }


Failed to load resource: the server responded with a status of 404 (Not Found) <- to pokazuje konsola w Chrome
Turson
I mamy zgadywać treść błędu facepalmxd.gif
Pyton_000
nie
new DateTime
tylko
new StupidDateTime
dominick
Problem rozwiązany :

pokręciłem z zamianą dateTime na StupidDateTime

Prawidłowo wystarczy zmienić tylko 1 linię :
  1. $older = new DateTime($userDTString);

na:
  1. $older = new StupidDateTime($userDTString);


Czyli klasę a nie zmienną jak to zrobiłem wcześniej.
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.