Funkcja formatująca datę na wczoraj, dzisiaj, ponad 2 tygodnie temu itd. 
Funkcja pobiera datę w formacie 000-00-00 00:00:00 i  zwraca ją w postaci dzisiaj, wczoraj, ponad 2 tygodnie temu, miesiąc temu itd.
Przykład użycia funkcji:
echo  formatujDate( date( "Y-m-d H:i:s", strtotime("-29 day")) ).'<br/>';
Zwróci nam: Ponad 2 tygodnie temu, 2010-03-09
Listing
<?php
/**
* Pod funkcja do funkcji formatującej date.
*
* @param string $minut
*/
function getMinutes($minut)
{
// j.pol
switch($minut)
{
case 0: return 0; break;
case 1: return 1; break;
case ($minut >= 2 && $minut <= 4):
case ($minut >= 22 && $minut <= 24):
case ($minut >= 32 && $minut <= 34):
case ($minut >= 42 && $minut <= 44):
case ($minut >= 52 && $minut <= 54): return "$minut minuty temu"; break;
default: return "$minut minut temu"; break;
}
return -1;
}
/**
* Formatuje date
*
* @param string $data_wejsciowa data w formacie 000-00-00 00:00:00
*/
function formatujDate($data_wejsciowa){
$timestamp = strtotime($data_wejsciowa);
$now = time();
if ($timestamp > $now) {
return 'Podana data nie może być większa od obecnej.';
}
$diff = $now - $timestamp;
$minut = floor($diff/60);
$godzin = floor($minut/60);
$dni = floor($godzin/24);
if ($minut <= 60) {
$res = getMinutes($minut);
switch($res)
{
case 0: return "przed chwilą";
case 1: return "minutę temu";
default: return $res;
}
}
$timestamp_wczoraj = $now-(60*60*24);
$timestamp_przedwczoraj = $now-(60*60*24*2);
if ($godzin > 0 && $godzin <= 6) {
$restMinutes = ($minut-(60*$godzin));
$res = getMinutes($restMinutes);
if ($godzin == 1) {
return "Godzinę temu ";//.$res
} else {
if ($godzin >1 && $godzin<5)return "$godzin godziny temu ";
if ($godzin >4)return "$godzin godzin temu";
}
} else if (date("Y-m-d", $timestamp) == date("Y-m-d", $now)) {//jesli dzisiaj
return "Dzisiaj ".date("H:i", $timestamp);
} else if (date("Y-m-d", $timestamp_wczoraj) == date("Y-m-d", $timestamp)) {//jesli wczoraj
return "Wczoraj ".date("H:i", $timestamp);
} else if (date("Y-m-d", $timestamp_przedwczoraj) == date("Y-m-d", $timestamp)) {//jesli wczoraj
return "Przedwczoraj ".date("H:i", $timestamp);
}
switch($dni)
{
case ($dni < 7): return "$dni dni temu, ".date("Y-m-d", $timestamp); break;
case 7: return "Tydzień temu, ".date("Y-m-d", $timestamp); break;
case ($dni > 7 && $dni < 14): return "Ponad tydzień temu, ".date("Y-m-d", $timestamp); break;
case 14: return "Dwa tygodznie temu, ".date("Y-m-d", $timestamp); break;
case ($dni > 14 && $dni < 30): return "Ponad 2 tygodnie temu, ".date("Y-m-d", $timestamp); break;
case 30: case 31: return "Miesiąc temu"; break;
case ($dni > 31): return date("Y-m-d H:i", $timestamp); break;
}
return date("Y-m-d", $timestamp);
}
//----------------Przykładowe Użycie
echo formatujDate( date( "Y-m-d H:i:s", strtotime("-29 day")) ).'<br/>';
echo formatujDate( date( "Y-m-d H:i:s", strtotime("-1 day")) ).'<br/>';
?>
        	/**
* Pod funkcja do funkcji formatującej date.
*
* @param string $minut
*/
function getMinutes($minut)
{
// j.pol
switch($minut)
{
case 0: return 0; break;
case 1: return 1; break;
case ($minut >= 2 && $minut <= 4):
case ($minut >= 22 && $minut <= 24):
case ($minut >= 32 && $minut <= 34):
case ($minut >= 42 && $minut <= 44):
case ($minut >= 52 && $minut <= 54): return "$minut minuty temu"; break;
default: return "$minut minut temu"; break;
}
return -1;
}
/**
* Formatuje date
*
* @param string $data_wejsciowa data w formacie 000-00-00 00:00:00
*/
function formatujDate($data_wejsciowa){
$timestamp = strtotime($data_wejsciowa);
$now = time();
if ($timestamp > $now) {
return 'Podana data nie może być większa od obecnej.';
}
$diff = $now - $timestamp;
$minut = floor($diff/60);
$godzin = floor($minut/60);
$dni = floor($godzin/24);
if ($minut <= 60) {
$res = getMinutes($minut);
switch($res)
{
case 0: return "przed chwilą";
case 1: return "minutę temu";
default: return $res;
}
}
$timestamp_wczoraj = $now-(60*60*24);
$timestamp_przedwczoraj = $now-(60*60*24*2);
if ($godzin > 0 && $godzin <= 6) {
$restMinutes = ($minut-(60*$godzin));
$res = getMinutes($restMinutes);
if ($godzin == 1) {
return "Godzinę temu ";//.$res
} else {
if ($godzin >1 && $godzin<5)return "$godzin godziny temu ";
if ($godzin >4)return "$godzin godzin temu";
}
} else if (date("Y-m-d", $timestamp) == date("Y-m-d", $now)) {//jesli dzisiaj
return "Dzisiaj ".date("H:i", $timestamp);
} else if (date("Y-m-d", $timestamp_wczoraj) == date("Y-m-d", $timestamp)) {//jesli wczoraj
return "Wczoraj ".date("H:i", $timestamp);
} else if (date("Y-m-d", $timestamp_przedwczoraj) == date("Y-m-d", $timestamp)) {//jesli wczoraj
return "Przedwczoraj ".date("H:i", $timestamp);
}
switch($dni)
{
case ($dni < 7): return "$dni dni temu, ".date("Y-m-d", $timestamp); break;
case 7: return "Tydzień temu, ".date("Y-m-d", $timestamp); break;
case ($dni > 7 && $dni < 14): return "Ponad tydzień temu, ".date("Y-m-d", $timestamp); break;
case 14: return "Dwa tygodznie temu, ".date("Y-m-d", $timestamp); break;
case ($dni > 14 && $dni < 30): return "Ponad 2 tygodnie temu, ".date("Y-m-d", $timestamp); break;
case 30: case 31: return "Miesiąc temu"; break;
case ($dni > 31): return date("Y-m-d H:i", $timestamp); break;
}
return date("Y-m-d", $timestamp);
}
//----------------Przykładowe Użycie
echo formatujDate( date( "Y-m-d H:i:s", strtotime("-29 day")) ).'<br/>';
echo formatujDate( date( "Y-m-d H:i:s", strtotime("-1 day")) ).'<br/>';
?>
Dodano przez: igor  
Ranga: Administrator serwisu Punktów: 0
Ranga: Administrator serwisu Punktów: 0
Komentarze użytkowników
	    fajnie działa :) i oto mi chodzilo 
	
	
		autor: mariolka | 12445 | 2011-05-22 16:39:39
	
:: Losowe artykuły 

:: Wymiana linków 
    
	Modowe inspiracje |
        Android Gry i Aplikacje |
        ZaplanujTransport.pl: Przeprowadzki, transport, aukcje |
        Logo dla firmy |
        Change Tires - Car Weather Forecast Reminder |
        Laminas: MVC Framework for PHP |
        IT Books Reviews and Programming: JS, JAVA, PHP, ANDROID, CSS | 
        Katalog roślin |
        Programming articles: JAVA, PHP, C++, Python, JavaScript |
        Kancelaria Adwokacka Łukasz Huszno
        
    

 
  
				

 
                             
 


 
                        
 
		 
	