Scripts

Datum Format Functie

Functie voor het omzetten van een datum (yyyy-mm-dd hh:mm:ss) naar een gewenst formaat (bijv. Maandag 25 November 2010 om 13u 15m). Function - fDate (Format Date) Function to format date and time to various output. Options to output preferred date and time stamp based on local $sDate - Date input in following format: yyyy-mm-dd hh:mm:ss $sFormat - Format to output date and time. Value: 1 through 9 Output (depending on SITE_LANGUAGE): SITE_LANGUAGE = 'EN': $sFormat = 1 -> 12/7/2010 $sFormat = 2 -> Tuesday 7 December 2010 $sFormat = 3 -> Tue 7 December 2010 $sFormat = 4 -> 4:21:35 PM $sFormat = 5 -> 12/7/2010 4:21:35 PM $sFormat = 6 -> Tuesday 7 December 2010 at 4:21:35 PM $sFormat = 7 -> Tue 7 December 2010 at 4:21:35 PM $sFormat = 8 -> Tuesday 7 December 2010 at 16h 21m $sFormat = 9 -> 12/7/2010 at 4:21:35 PM SITE_LANGUAGE = 'NL': $sFormat = 1 -> 7-12-2010 $sFormat = 2 -> Dinsdag 7 december 2010 $sFormat = 3 -> Di 7 december 2010 $sFormat = 4 -> 16:21:35 $sFormat = 5 -> 7-12-2010 16:21:35 $sFormat = 6 -> Dinsdag 7 december 2010 om 16:21:35 $sFormat = 7 -> Di 7 december 2010 om 16:21:35 $sFormat = 8 -> Dinsdag 7 december 2010 om 16u 21m $sFormat = 9 -> 7-12-2010 om 16:21:35 Handig voor het weergeven van datums uit een database dus!

format_date.php
<?php

// Set country / language
$lang = 'NL';
define('SITE_LANGUAGE', $lang); // Site language (EN,NL,etc.)

function fDate($sDate,$sFormat) {

	/**
	*
	*	Function - fDate (Format Date)
	*
	*	Function to format date and time to various output.
	*	Options to output preferred date and time stamp based on local
	*
	*	$sDate - Date input in following format: yyyy-mm-dd hh:mm:ss
	*	$sFormat - Format to output date and time. Value: 1 through 9
	*
	*	Output (depending on SITE_LANGUAGE):
	*	$sFormat = 1 -> 12/7/2010
	*	$sFormat = 2 -> Tuesday 7 December 2010
	*	$sFormat = 3 -> Tue 7 December 2010
	*	$sFormat = 4 -> 4:21:35 PM
	*	$sFormat = 5 -> 12/7/2010 4:21:35 PM
	*	$sFormat = 6 -> Tuesday 7 December 2010 at 4:21:35 PM
	*	$sFormat = 7 -> Tue 7 December 2010 at 4:21:35 PM
	*	$sFormat = 8 -> Tuesday 7 December 2010 at 16h 21m
	*	$sFormat = 9 -> 12/7/2010 at 4:21:35 PM
	*
	**/

	// Seperate date and time
	$pieces = explode(' ',$sDate);
	$date = $pieces[0];
	$time = $pieces[1];

	// Seperate year, month and day
	$pieces = explode('-',$date);
	$year = $pieces[0];
	$month = $pieces[1];
	$day = $pieces[2];
	
	// Seperate hour, minute and second
	$pieces = explode(':',$time);
	$hour = $pieces[0];
	$minute = $pieces[1];
	$second = $pieces[2];
	
   // Set country / language
    switch(SITE_LANGUAGE) {
        case 'NL':
            /* Set locale to Dutch */
            setlocale(LC_ALL, 'nld_nld');
            /* Language adjustments */
            $at = 'om';
            $h = 'u';
            $m = 'm';
        break;
        default:
            /* Set locale to English */
            setlocale(LC_ALL, 'En-Us');
            /* Language adjustments */
            $at = 'at';
            $h = 'h';
            $m = 'm';
        break;
    }

	// Return format
	switch($sFormat) {

		#==== Date only ====#
		case '1':
			return strftime("%x", mktime(0, 0, 0, $month, $day, $year));
		break;
		case '2':
			return strftime("%A %#d %B %Y", mktime(0, 0, 0, $month, $day, $year));
		break;
		case '3':
			return strftime("%a %#d %B %Y", mktime(0, 0, 0, $month, $day, $year));
		break;

		#==== Time only ====#
		case '4':
			return strftime("%X", mktime($hour, $minute, $second, $month, $day, $year));
		break;

		#==== Date and time ====#
		case '5':
			return strftime("%c",mktime($hour, $minute, $second, $month, $day, $year));
		break;
		case '6':
			return strftime("%A %#d %B %Y ".$at." %X", mktime($hour, $minute, $second, $month, $day, $year));
		break;
		case '7':
			return strftime("%a %#d %B %Y ".$at." %X", mktime($hour, $minute, $second, $month, $day, $year));
		break;
		case '8':
			return strftime("%A %#d %B %Y ".$at." %H".$h." %M".$m, mktime($hour, $minute, $second, $month, $day, $year));
		break;
		case '9';
			return strftime("%x ".$at." %X", mktime($hour, $minute, $second, $month, $day, $year));
		break;
	}
}

// How to use
$date_posted = '2010-12-06 21:38:35';
echo '<p>' . ucfirst(fDate($date_posted,'1')).'</p>';

// Show all formats:
for($x=1;$x<10;$x++) {
echo '<p class="mailInfo">$sFormat = '.$x.' -> ' . ucfirst(fDate($date_posted,$x)).'</p>';
}
?>

Reacties

0
Nog geen reacties.