format_date.php

Gesponsorde koppelingen

PHP script bestanden

  1. format_date.php

« Lees de omschrijving en reacties

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?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>';
}

?>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.