Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [KOHANA] Przekształcenie daty na "dzisiaj" lub "wczoraj"
dominick
post
Post #1





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

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


Zamiana daty typu dd-mm-YYYY na np. "dzisiaj" gdy data jest taka sama jak dzisiejsza.

datę wyświetlam czymś takim
  1. <div>
  2. <span class="date_added">
  3. <span>
  4. <?php echo date::my($a->annoucement_date_added, 'announcement') ?>
  5. </span>
  6. </span>
  7. </div>


Znalazłem jeszcze w kontrolerze daty coś odpowiedzialnego za format:
  1. class Date extends I18n_Date {
  2.  
  3. public static function my($value, $type = NULL)
  4. {
  5. $format = NULL;
  6.  
  7. if ($type !== NULL)
  8. {
  9. $format = Kohana::$config->load('date.' . $type);
  10. }
  11.  
  12. if ($format === NULL)
  13. {
  14. $format = 'd-m-Y';
  15. }
  16.  
  17. if (is_numeric($value))
  18. {
  19. $date = date($format, $value);
  20. }
  21. else
  22. {
  23. $date = strtotime($value);
  24. $date = date($format, $date);
  25. }
  26. return $date;
  27. }
  28.  
  29.  
  30. /**
  31. * Returns time difference between two timestamps, in human readable format.
  32. * If the second timestamp is not given, the current time will be used.
  33. * Also consider using [Date::fuzzy_span] when displaying a span.
  34. *
  35. * $span = Date::span(60, 182, 'minutes,seconds'); // array('minutes' => 2, 'seconds' => 2)
  36. * $span = Date::span(60, 182, 'minutes'); // 2
  37. *
  38. * @param integer $remote timestamp to find the span of
  39. * @param integer $local timestamp to use as the baseline
  40. * @param string $output formatting string
  41. * @return string when only a single output is requested
  42. * @return array associative list of all outputs requested
  43. */
  44. public static function span($remote, $local = NULL, $output = 'years,months,weeks,days,hours,minutes,seconds')
  45. {
  46. // Normalize output
  47. $output = trim(strtolower( (string) $output));
  48.  
  49. if ( ! $output)
  50. {
  51. // Invalid output
  52. return FALSE;
  53. }
  54.  
  55. // Array with the output formats
  56. $output = preg_split('/[^a-z]+/', $output);
  57.  
  58. // Convert the list of outputs to an associative array
  59. $output = array_combine($output, array_fill(0, count($output), 0));
  60.  
  61. //Fix bug: Only variables should be passed by reference
  62. $fliped = array_flip($output);
  63.  
  64. // Make the output values into keys
  65. extract($fliped, EXTR_SKIP);
  66.  
  67. if ($local === NULL)
  68. {
  69. // Calculate the span from the current time
  70. $local = time();
  71. }
  72.  
  73. // Calculate timespan (seconds)
  74. $timespan = abs($remote - $local);
  75.  
  76. if (isset($output['years']))
  77. {
  78. $timespan -= Date::YEAR * ($output['years'] = (int) floor($timespan / Date::YEAR));
  79. }
  80.  
  81. if (isset($output['months']))
  82. {
  83. $timespan -= Date::MONTH * ($output['months'] = (int) floor($timespan / Date::MONTH));
  84. }
  85.  
  86. if (isset($output['weeks']))
  87. {
  88. $timespan -= Date::WEEK * ($output['weeks'] = (int) floor($timespan / Date::WEEK));
  89. }
  90.  
  91. if (isset($output['days']))
  92. {
  93. $timespan -= Date::DAY * ($output['days'] = (int) floor($timespan / Date::DAY));
  94. }
  95.  
  96. if (isset($output['hours']))
  97. {
  98. $timespan -= Date::HOUR * ($output['hours'] = (int) floor($timespan / Date::HOUR));
  99. }
  100.  
  101. if (isset($output['minutes']))
  102. {
  103. $timespan -= Date::MINUTE * ($output['minutes'] = (int) floor($timespan / Date::MINUTE));
  104. }
  105.  
  106. // Seconds ago, 1
  107. if (isset($output['seconds']))
  108. {
  109. $output['seconds'] = $timespan;
  110. }
  111.  
  112. if (count($output) === 1)
  113. {
  114. // Only a single output was requested, return it
  115. return array_pop($output);
  116. }
  117.  
  118. // Return array
  119. return $output;
  120. }
  121.  
  122. }
  123.  


Starałem się użyć jakiejś funkcji ale za każdym razem błąd - nie mam już sił proszę o pomoc.
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 2)
Spawnm
post
Post #2





Grupa: Moderatorzy
Postów: 4 069
Pomógł: 497
Dołączył: 11.05.2007
Skąd: Warszawa




Najprościej to będziesz mieć:
  1. $date = ($date==date('Y-m-d'))? 'Dzisiaj': $date;
Go to the top of the page
+Quote Post
dominick
post
Post #3





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

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


Dzięki tylko jest mały problem bo wyświetlam datę tak jak napisałem na początku. Sęk w tym jak porównać tę datę w dzisiejszą i gdy jest taka sama to wyświetli Dzisiaj.
skonstruowałem takiego i wyskakuje błąd

  1. <?php $date == (date::my($a->annoucement_date_added, 'announcement'))? 'Dzisiaj': $date; ?>
  2. <?php echo $date ?>


znalazłęm fajną funkcję, która idealnie wpasowałaby się w to czego szukam jednak nie mam pojęcia jak ją wpisać w kod.
  1. <?php
  2. /**
  3. * Pod funkcja do funkcji formatującej date.
  4. *
  5. * @param string $minut
  6. */
  7.  
  8. function getMinutes($minut)
  9. {
  10. // j.pol
  11. switch($minut)
  12. {
  13. case 0: return 0; break;
  14. case 1: return 1; break;
  15. case ($minut >= 2 && $minut <= 4):
  16. case ($minut >= 22 && $minut <= 24):
  17. case ($minut >= 32 && $minut <= 34):
  18. case ($minut >= 42 && $minut <= 44):
  19. case ($minut >= 52 && $minut <= 54): return "$minut minuty temu"; break;
  20. default: return "$minut minut temu"; break;
  21. }
  22. return -1;
  23. }
  24.  
  25. /**
  26. * Formatuje date
  27. *
  28. * @param string $data_wejsciowa data w formacie 000-00-00 00:00:00
  29. */
  30.  
  31. function formatujDate($data_wejsciowa){
  32.  
  33. $timestamp = strtotime($data_wejsciowa);
  34.  
  35. $now = time();
  36.  
  37. if ($timestamp > $now) {
  38. return 'Podana data nie może być większa od obecnej.';
  39. }
  40.  
  41. $diff = $now - $timestamp;
  42.  
  43. $minut = floor($diff/60);
  44. $godzin = floor($minut/60);
  45. $dni = floor($godzin/24);
  46.  
  47. if ($minut <= 60) {
  48. $res = getMinutes($minut);
  49. switch($res)
  50. {
  51. case 0: return "przed chwilą";
  52. case 1: return "minutę temu";
  53. default: return $res;
  54. }
  55. }
  56.  
  57. $timestamp_wczoraj = $now-(60*60*24);
  58. $timestamp_przedwczoraj = $now-(60*60*24*2);
  59.  
  60. if ($godzin > 0 && $godzin <= 6) {
  61.  
  62. $restMinutes = ($minut-(60*$godzin));
  63. $res = getMinutes($restMinutes);
  64. if ($godzin == 1) {
  65. return "Godzinę temu ";//.$res
  66. } else {
  67. if ($godzin >1 && $godzin<5)return "$godzin godziny temu ";
  68. if ($godzin >4)return "$godzin godzin temu";
  69. }
  70.  
  71. } else if (date("Y-m-d", $timestamp) == date("Y-m-d", $now)) {//jesli dzisiaj
  72. return "Dzisiaj ".date("H:i", $timestamp);
  73. } else if (date("Y-m-d", $timestamp_wczoraj) == date("Y-m-d", $timestamp)) {//jesli wczoraj
  74. return "Wczoraj ".date("H:i", $timestamp);
  75. } else if (date("Y-m-d", $timestamp_przedwczoraj) == date("Y-m-d", $timestamp)) {//jesli wczoraj
  76. return "Przedwczoraj ".date("H:i", $timestamp);
  77. }
  78.  
  79. switch($dni)
  80. {
  81. case ($dni < 7): return "$dni dni temu, ".date("Y-m-d", $timestamp); break;
  82. case 7: return "Tydzień temu, ".date("Y-m-d", $timestamp); break;
  83. case ($dni > 7 && $dni < 14): return "Ponad tydzień temu, ".date("Y-m-d", $timestamp); break;
  84. case 14: return "Dwa tygodznie temu, ".date("Y-m-d", $timestamp); break;
  85. case ($dni > 14 && $dni < 30): return "Ponad 2 tygodnie temu, ".date("Y-m-d", $timestamp); break;
  86. case 30: case 31: return "Miesiąc temu"; break;
  87. case ($dni > 31): return date("Y-m-d H:i", $timestamp); break;
  88. }
  89. return date("Y-m-d", $timestamp);
  90. }


Przepraszam za ott ale czy mógłby mi ktoś pomóc wstawić powyższy kod pod to co mam na stronie. Błagam

Nikt nie wie jak to ugryźć?

Ten post edytował dominick 5.09.2014, 19:41:41
Go to the top of the page
+Quote Post

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

 



RSS Aktualny czas: 21.08.2025 - 22:38