PHPBuilder - custom user format function



RSS Twitter
Snippets Calendars Dates

custom user format function

by: Tony Bibbs
|
October 1, 2001

Version: 1.0

Type: Function

Category: Calendars/Dates

License: GNU General Public License

Description: If you have a web application where you have many user accounts you can let the user customize the format of their date and time. In your user->preferences screen within your application add a drop down of possible date/time formats and store it in your users table. This function will read the format from that table and format it and return the formated time and unix timestamp in case you may need it. The date/time you pass this function can be in almost any format (english string, timestamp) and if you don't pass a date to this function it will return the current date/time in the user-specified format. You'll also see the code will read a default value from a config file just in case the user doesn't specify a format.



#This function takes a date in either unixtimestamp or in english and 
#formats it to the users preference.  If the user didn't specify a format
#the format in the config file is used.  This returns array where array[0]
# is the formated date and array[1] is the unixtimestamp
function getuserdatetimeformat($date="") {
        global $USER,$CONF;

        #Get display format for time
        if ($USER["uid"] > 1) {
                $result = dbquery("SELECT format FROM {$CONF["db_prefix"]}dateformats, {$CONF["db_prefix"]}userprefs WHERE dateformats.dfid = userprefs.dfid AND uid = {$USER["uid"]}");
                $nrows = mysql_num_rows($result);
                $A = mysql_fetch_array($result);
                if (empty($A["format"])) {
                        $dateformat = $CONF["date"];
                } else {
                        $dateformat = $A["format"];
                }
        } else {
                $dateformat = $CONF["date"];
        }

        if (empty($date)) {
                #date is empty, get current date/time
                $stamp = time();
        } else if (is_numeric($date)) {
                #this is a timestamp
                $stamp = $date;
        } else {
                #this is a string representation of a date/time
                $stamp = strtotime($date);
        }

        # Actually format the date now
        $date = strftime($dateformat,$stamp);
        return array($date, $stamp);
}

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Tony Bibbs

Comment:



Comment:

(Maximum characters: 1200). You have characters left.