Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> PRoblem z funkcją formatującą datę
dominick
post
Post #1





Grupa: Zarejestrowani
Postów: 88
Pomógł: 0
Dołączył: 30.05.2014

Ostrzeżenie: (0%)
-----


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') ?>

Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
kalipek
post
Post #2





Grupa: Zarejestrowani
Postów: 43
Pomógł: 13
Dołączył: 3.02.2011

Ostrzeżenie: (0%)
-----


  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');
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 14.10.2025 - 05:51