16. Oktober 2010
start
»
network
» programming
comments 0
PHP: niceDate Function
I always need to convert dates in PHP. To make my life easier i build myself a niceDate Function.
Feel free to grab the Code and use it at your Programming Projects. The Function accepts 3 Parameters: a Timestamp, a Styling Parameter (bol/int) and a Local Setting (e.g. de_DE).
The niceDate Format can look like
- DD.MM.YYYY HH:MM
- DD.MM.YYYY HH:MM:SS
- DD.MM.YYYY
- HH:MM
- DD Month YYYY
- MONDAY, DD Month YYYY
/**
* niceDate - convert a timestamp to a nice layout
* @param string, $date, timestamp
* @param int/bol $style - date layout, false for DD.MM.YYYY HH:MM, 1 for DD.MM.YYYY HH:MM:SS, 2 for DD.MM.YYYY, 3 for HH:MM, 4 for DD Month YYYY, 5 textual day, DD Month YYYY
* @param string $local - default 'de_DE'
* @return string
*/
function niceDate($date, $style=false, $local='de_DE'){
setlocale(LC_ALL, $local);
$dateUnix = strtotime($date);
if($dateUnix == '-1')
$dateUnix = strtotime(
sprintf(
"%02s-%02s-%02s %02s:%02s:%02s",
substr($date,0,4),
substr($date,4,2),
substr($date,6,2),
substr($date,8,2),
substr($date,10,2),
substr($date,12,2)
)
);
if($style==false)
$date_nice = strftime('%d.%b.%Y, %H:%M', $dateUnix);
elseif($style=='1')
$date_nice = strftime('%d.%b.%Y, %H:%M:%S', $dateUnix);
elseif($style=='2')
$date_nice = strftime('%d.%b. %Y', $dateUnix);
elseif($style=='3')
$date_nice = strftime('%H:%M', $dateUnix);
elseif($style=='4')
$date_nice = strftime('%d %B %Y', $dateUnix);
elseif($style=='5')
$date_nice = strftime('%A, %d. %b %Y', $dateUnix);
return $date_nice;
}
Read other articles
- Flyweb NetNewsWire Theme
- Textpattern & PHP
- Automatic Browser reloading while Programming
- mySQL: Update or Insert

Spread The Word →
0 Comment/s
No Comments yet, be the first who posts a comment!