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().
| Key | Description | Example |
| d | Day of the month, 2 digits with leading zeros | 01 to 31 |
| D | A textual representation of a day, three letters | Mon through Sun |
| j | Day of the month without leading zeros | 1 to 31 |
| l | A full textual representation of the day of the week | Sunday through Saturday |
| N | ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0) | 1 (for Monday) through 7 (for Sunday) |
| S | English ordinal suffix for the day of the month, 2 characters | st, nd, rd or th. Works well with j |
| w | Numeric representation of the day of the week | 0 (for Sunday) through 6 (for Saturday) |
| z | The day of the year (starting from 0) | 0 through 365 |
| W | ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0) | Example: 42 (the 42nd week in the year) |
| F | A full textual representation of a month, such as January or March | January through December |
| m | Numeric representation of a month, with leading zeros | 01 through 12 |
| M | A short textual representation of a month, three letters | Jan through Dec |
| n | Numeric representation of a month, without leading zeros | 1 through 12 |
| t | Number of days in the given month | 28 through 31 |
| L | Whether it's a leap year | 1 if it is a leap year, 0 otherwise. |
| o | 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. (added in PHP 5.1.0) | Examples: 1999 or 2003 |
| Y | A full numeric representation of a year, 4 digits | Examples: 1999 or 2003 |
| y | A two digit representation of a year | Examples: 99 or 03 |
| a | Lowercase Ante meridiem and Post meridiem | am or pm |
| A | Uppercase Ante meridiem and Post meridiem | AM or PM |
| g | 12-hour format of an hour without leading zeros | 1 through 12 |
| G | 24-hour format of an hour without leading zeros | 0 through 23 |
| h | 12-hour format of an hour with leading zeros | 01 through 12 |
| H | 24-hour format of an hour with leading zeros | 00 through 23 |
| i | Minutes with leading zeros | 00 to 59 |
| s | Seconds, with leading zeros | 00 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
{
var s, d, retval, temp;
if (argument1 == 0)
{
d = date_current_datetime();
}
else
{
d = argument1;
}
s = argument0;
retval = "";
while (s != "")
{
switch (string_copy(s, 1, 1))
{
case "d":
temp = string(date_get_day(d));
retval += string_repeat("0", 2 - string_length(temp)) + temp;
break;
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;
case "j":
retval += string(date_get_day(d));
break;
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;
case "N":
temp = date_get_weekday(d) - 1;
if (temp == 0)
{
temp = 7;
}
retval += string(temp);
break;
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;
case "w":
retval += string(date_get_weekday(d) - 1);
break;
case "z":
retval += string(date_get_day_of_year(d) - 1);
break;
case "W":
retval += string(date_get_week(d));
break;
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;
case "m":
temp = string(date_get_month(d));
retval += string_repeat("0", 2 - string_length(temp)) + temp;
break;
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;
case "n":
retval += string(date_get_month(d));
break;
case "t":
retval += string(date_days_in_month(d));
break;
case "L":
retval += string(date_leap_year(d));
break;
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;
case "Y":
retval += string(date_get_year(d));
break;
case "y":
temp = string(date_get_year(d) mod 100);
retval += string_repeat("0", 2 - string_length(temp)) + temp;
break;
case "a":
if (date_get_hour(d) < 12)
{
retval += "am";
}
else
{
retval += "pm";
}
break;
case "A":
if (date_get_hour(d) < 12)
{
retval += "AM";
}
else
{
retval += "PM";
}
break;
case "g":
temp = date_get_hour(d) mod 12;
if (temp == 0)
{
temp = 12;
}
retval += string(temp);
break;
case "G":
retval += string(date_get_hour(d));
break;
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;
case "H":
temp = string(date_get_hour(d));
if (temp == "0")
{
retval += "00";
}
else
{
retval += string_repeat("0", 2 - string_length(temp)) + temp;
}
break;
case "i":
temp = string(date_get_minute(d));
retval += string_repeat("0", 2 - string_length(temp)) + temp;
break;
case "s":
temp = string(date_get_second(d));
retval += string_repeat("0", 2 - string_length(temp)) + temp;
break;
case "\":
s = string_delete(s, 1, 1);
default:
retval += string_copy(s, 1, 1);
break;
}
s = string_delete(s, 1, 1);
}
return retval;
}