phpInDev

date

string date ( string date_string, [ datetime time ] )

Date function is similar to PHP's date function. Basically this a GML implementation of that function with few differences (due to GM's limitations).

The function takes a string as an argument. This string consists of certain key characters. Each key character is expanded into a some value of time. for example, giving "Y" as the date_string would simply return "2005". See below for full list of key characters. The second argument for the function is optimal. This is the datetime number to determine the values for the key characters. If no value is given (or 0 is given), the function will use the value returned by date_current_datetime().

KeyDescriptionExample
dDay of the month, 2 digits with leading zeros01 to 31
DA textual representation of a day, three lettersMon through Sun
jDay of the month without leading zeros1 to 31
lA full textual representation of the day of the weekSunday through Saturday
NISO-8601 numeric representation of the day of the week (added in PHP 5.1.0)1 (for Monday) through 7 (for Sunday)
SEnglish ordinal suffix for the day of the month, 2 charactersst, nd, rd or th. Works well with j
wNumeric representation of the day of the week0 (for Sunday) through 6 (for Saturday)
zThe day of the year (starting from 0)0 through 365
WISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0)Example: 42 (the 42nd week in the year)
FA full textual representation of a month, such as January or MarchJanuary through December
mNumeric representation of a month, with leading zeros01 through 12
MA short textual representation of a month, three lettersJan through Dec
nNumeric representation of a month, without leading zeros1 through 12
tNumber of days in the given month28 through 31
LWhether it's a leap year1 if it is a leap year, 0 otherwise.
oISO-8601 year number. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0)Examples: 1999 or 2003
YA full numeric representation of a year, 4 digitsExamples: 1999 or 2003
yA two digit representation of a yearExamples: 99 or 03
aLowercase Ante meridiem and Post meridiemam or pm
AUppercase Ante meridiem and Post meridiemAM or PM
g12-hour format of an hour without leading zeros1 through 12
G24-hour format of an hour without leading zeros0 through 23
h12-hour format of an hour with leading zeros01 through 12
H24-hour format of an hour with leading zeros00 through 23
iMinutes with leading zeros00 to 59
sSeconds, with leading zeros00 through 59

Copied from PHP's manual

If you wish to use the actual characters in the string instead of expanding them into other values, you may escape the characters with a backlash. For example, "\Ye\ar: Y" would return "Year: 2005". Note that backslash always escapes the next character, whether it is a key character or not. To use backslash in the string, simply use double backslash "\\" (as the first backslash will escape the second one).

Note: There are few differences to the PHP's date function. Mainly, the following key characters are missing due to GM's limitations: B, e, I, O, T, Z, c, r, U.

Note: As the function uses the built in datetime functions, any real value inaccuracy bug affecting GM6 will also have similar effects on this script and it can not be avoided.

Example 1. date

{ show_message(date("T\o\d\a\y \i\s: jS F Y")); }

Shows message:

Today is: 24th December 2005

Example 2. date

{ // Note the GM6's inaccuracy bugs will likely cause this to display wrong time show_message(date("H:i:s d/m/Y", date_create_datetime(2003, 6, 7, 12, 50, 0))); }

Should show message:

12:50:00 07/06/2003

Code for date.gml

{
    var s, d, retval, temp;
    
    // Either use given datetime, or get the current moment
    if (argument1 == 0)
    {
        d = date_current_datetime();
    }
    else
    {
        d = argument1;
    }
    
    s = argument0;
    
    retval = "";
    
    // Loop till all is clear
    while (s != "")
    {
        switch (string_copy(s, 1, 1))
        {
            // Day of the month, 2 digits with leading zeros
            case "d":
                temp = string(date_get_day(d));
                retval += string_repeat("0", 2 - string_length(temp)) + temp;
                break;
                
            // A textual representation of a day, three letters
            case "D":
                switch (date_get_weekday(d))
                {
                    case 1:
                        retval += "Sun";
                        break;
                    case 2:
                        retval += "Mon";
                        break;
                    case 3:
                        retval += "Tue";
                        break;
                    case 4:
                        retval += "Wed";
                        break;
                    case 5:
                        retval += "Thu";
                        break;
                    case 6:
                        retval += "Fri";
                        break;
                    case 7:
                        retval += "Sat";
                        break;
                }
                break;
                
            // Day of the month without leading zeros
            case "j":
                retval += string(date_get_day(d));
                break;
            
            // A full textual representation of the day of the week
            case "l":
                switch (date_get_weekday(d))
                {
                    case 1:
                        retval += "Sunday";
                        break;
                    case 2:
                        retval += "Monday";
                        break;
                    case 3:
                        retval += "Tuesday";
                        break;
                    case 4:
                        retval += "Wednesday";
                        break;
                    case 5:
                        retval += "Thursday";
                        break;
                    case 6:
                        retval += "Friday";
                        break;
                    case 7:
                        retval += "Saturday";
                        break;
                }
                break;
                
            // ISO-8601 numeric representation of the day of the week
            case "N":
                temp = date_get_weekday(d) - 1;
                if (temp == 0)
                {
                    temp = 7;
                }
                retval += string(temp);
                break;
                    
            // English ordinal suffix for the day of the month, 2 characters
            case "S":
                switch (date_get_day(d))
                {
                    case 1:
                    case 21:
                    case 31:
                        retval += "st";
                        break;
                    case 2:
                    case 22:
                        retval += "nd";
                        break;
                    case 3:
                    case 23:
                        retval += "rd";
                        break;
                    default:
                        retval += "th";
                        break;
                }
                break;
            
            // Numeric representation of the day of the week
            case "w":
                retval += string(date_get_weekday(d) - 1);
                break;
                
            // The day of the year (starting from 0)
            case "z":
                retval += string(date_get_day_of_year(d) - 1);
                break;
                
            // ISO-8601 week number of year, weeks starting on Monday
            case "W":
                retval += string(date_get_week(d));
                break;
                
            // A full textual representation of a month, such as January or March
            case "F":
                switch (date_get_month(d))
                {
                    case 1:
                        retval += "January";
                        break;
                    case 2:
                        retval += "February";
                        break;
                    case 3:
                        retval += "March";
                        break;
                    case 4:
                        retval += "April";
                        break;
                    case 5:
                        retval += "May";
                        break;
                    case 6:
                        retval += "June";
                        break;
                    case 7:
                        retval += "July";
                        break;
                    case 8:
                        retval += "August";
                        break;
                    case 9:
                        retval += "September";
                        break;
                    case 10:
                        retval += "October";
                        break;
                    case 11:
                        retval += "November";
                        break;
                    case 12:
                        retval += "December";
                        break;
                }
                break;
                
            // Numeric representation of a month, with leading zeros
            case "m":
                temp = string(date_get_month(d));
                retval += string_repeat("0", 2 - string_length(temp)) + temp;
                break;
                
            // A short textual representation of a month, three letters
            case "M":
                switch (date_get_month(d))
                {
                    case 1:
                        retval += "Jan";
                        break;
                    case 2:
                        retval += "Feb";
                        break;
                    case 3:
                        retval += "Mar";
                        break;
                    case 4:
                        retval += "Apr";
                        break;
                    case 5:
                        retval += "May";
                        break;
                    case 6:
                        retval += "Jun";
                        break;
                    case 7:
                        retval += "Jul";
                        break;
                    case 8:
                        retval += "Aug";
                        break;
                    case 9:
                        retval += "Sep";
                        break;
                    case 10:
                        retval += "Oct";
                        break;
                    case 11:
                        retval += "Nov";
                        break;
                    case 12:
                        retval += "Dec";
                        break;
                }
                break;
                
            // Numeric representation of a month, without leading zeros
            case "n":
                retval += string(date_get_month(d));
                break;
                
            // Number of days in the given month
            case "t":
                retval += string(date_days_in_month(d));
                break;
                
            // Whether it's a leap year
            case "L":
                retval += string(date_leap_year(d));
                break;
                
            // ISO-8601 year number. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead
            case "o":
                if (date_get_day_of_year(d) <= 7 && date_get_week(d) != 1)
                {
                    retval += string(date_get_year(d) - 1);
                }
                else
                {
                    retval += string(date_get_year(d));
                }
                break;
                
            // A full numeric representation of a year, 4 digits
            case "Y":
                retval += string(date_get_year(d));
                break;
                
            // A full numeric representation of a year, 4 digits
            case "y":
                temp = string(date_get_year(d) mod 100);
                retval += string_repeat("0", 2 - string_length(temp)) + temp;
                break;
                
            // Lowercase Ante meridiem and Post meridiem
            case "a":
                if (date_get_hour(d) < 12)
                {
                    retval += "am";
                }
                else
                {
                    retval += "pm";
                }
                break;
            
            // Uppercase Ante meridiem and Post meridiem
            case "A":
                if (date_get_hour(d) < 12)
                {
                    retval += "AM";
                }
                else
                {
                    retval += "PM";
                }
                break;
                
            // 12-hour format of an hour without leading zeros
            case "g":
                temp = date_get_hour(d) mod 12;
                if (temp == 0)
                {
                    temp = 12;
                }
                retval += string(temp);
                break;
                
            // 24-hour format of an hour without leading zeros
            case "G":
                retval += string(date_get_hour(d));
                break;
                
            // 12-hour format of an hour with leading zeros
            case "h":
                temp = string(date_get_hour(d) mod 12);
                if (temp == "0")
                {
                    temp = "12";
                }
                retval += string_repeat("0", 2 - string_length(temp)) + temp;
                break;
                
            // 24-hour format of an hour with leading zeros
            case "H":
                temp = string(date_get_hour(d));
                if (temp == "0")
                {
                    retval += "00";
                }
                else
                {
                    retval += string_repeat("0", 2 - string_length(temp)) + temp;
                }
                break;
                
            // Minutes with leading zeros
            case "i":
                temp = string(date_get_minute(d));
                retval += string_repeat("0", 2 - string_length(temp)) + temp;
                break;
                
            // Seconds, with leading zeros
            case "s":
                temp = string(date_get_second(d));
                retval += string_repeat("0", 2 - string_length(temp)) + temp;
                break;        
                
            // Backslash escapes chars
            case "\":
                s = string_delete(s, 1, 1);
            default:
                retval += string_copy(s, 1, 1);
                break;                
        }
                
        s = string_delete(s, 1, 1);
    }
    
    return retval;
}