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
dominick
post
Post #2





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

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


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

Ten post edytował dominick 16.09.2014, 15:43:17
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: 28.12.2025 - 16:38