This chapter will study existing functions in PHP for employment dates. Such functions exist in most programming languages and are aimed at obtaining and its representation in different formats.
The weather in any programming language is usually taken with regard to the opening of the “era UNIX” which is January 1, 1970 at 00:00:00. The simplest function que se basa en mark this time is the time function () whose return value is the number that represents the whole time stamp for the moment when the function was implemented regarding the era unix.
In some applications it is necessary to have a time stamp more detailed and therefore we use microtime () which returns a string with seconds and microseconds.
Should we wish to have as a benchmark when the computer from which runs employment function gettimeofday () in which being printed out internal sec (to find out the seconds), USEC (microseconds), minuteswest (No second west of Greenwich) and dstime (type of correction in times of summer and winter).
These features mentioned above are rarely used since the existence of other functions more complete, such as the role getdate () you get a matrix of partnership with the date information and time system. The elements of this matrix are:
Number of seconds seconds of the current time
Number of minutes minutes from the current time
Number of hours hours of the current time
mday corresponding day of the month
wday Weekday in numerical value (starting with 0)
mon Month numerico.Del year in value from 1 to 12.
Year numerical value of the year
yday Year’s Day in numerical value
weekday string containing the day of the week (in English)
month string of characters that contains the month of the year (in English)
0 Make time obtained by the function getdate ()
If you do not spend any parameter to the function then considers the current time system and if it receives as a parameter an integer then converts it to the relevant date.
Another function is the time to get the role localtime (marca_tiempo, tipo_matriz); whose values we turn to comment below:
Content key indicator
0 tm_sec Number of seconds of the date indicated
1 tm_min Number of minutes from the date indicated
2 tm_hour Number of hours of the date indicated
Day 3 tm_mday corresponding month
4 tm_wday Weekday in numerical value (starting with 0)
5 tm_mon Month of the year in value numerico.Del 0 to 11.
6 tm_year numerical value of the year. (Is affected by the effect 2000)
7 tm_yday Year’s Day in numerical value
8 tm_isdst Indicates if this enabled the effect of the change of time.
The functions previously allowed to view the whole value of converting the date at a value more easily understandable, but to gain access to such information must be passed through the preliminary step of obtaining a parent. To avoid this intermediate step, PHP available to you the funcióndate (format, marca_tiempo);
This feature takes us back a string that corresponds to a date to which it has implemented a certain format. To set the date format will have the following choices:
It is to show that when the chain AM / PM
In ago that when you see the chain AM / PM
d Day of the month with two digits from 01 to 31
D Day week as a string of three letters (in English). Example: “Mon”
F name full month as a string of caracteres.Ejemplo: “March”
h ago that the time appears in 01 to 12
M ago that the time appears in 00 to 23
g ago that the time appears as a 1 to 12
G ago that the time appears as a 0 to 23
i minutes ago that appear in 00 to 59
j ago that the day appears as a 1 to 31
l (H min) Weekday completo.Ejemplo: Monday
L wrote 0 if not leap year and 1 if it is
m month ago that appear in 01 to 12
M ago that appears in the month from 1 to 12
It s the latter appearing in 00 to 59
S Chain characters with the suffix ordinal.Ejemplo: “th”, “nd”.
t Number of days of the month specified 28 to 31
U number of seconds since the beginning of the “era UNIX”
w number weekday from 0 to 6
And with four-digit Year
and with double-digit Year
z Year’s Day of 0 to 365
Z Gets the time difference in seconds with respect to GMT
The role strftime () represents another possibility to implement a date formats. This function uses the conventions of the local machine that runs from the script to return a string in the format defined in the selected language. Its format is defined by the following values:
% Name weekday abbreviated in the current language
% A Name weekday complete the current language
% b month abbreviated name in the current language
% B name full month in the current language
% c Representation of date and time in the current language
% d Day of the month in 01 to 31
% M Time as number 01 to 12
I% Time as number 01 to 12
% j day of the year as number 001 to 366
% m Month as a number from 01 to 12
% M Minute number
% w am or pm depending on the time given
Seconds S% BY NUMBER
U% Number of the week of the year as the first Sunday as the first day of the week
W% Number of the week of the year as the first Monday as the first day of the week
% w Weekday in number from 0 to 6
x% Representation default date without a time
X% Representation default when undated
% Year in number from 00 to 99
And% Year in four-digit number
% Z name or abbreviation of the time zone
Setting times and dates
Once known how to obtain the current date, it is necessary to have a way to fix a certain time to establish such expiration date of a cookie, namely how to get a time stamp for a given hour.
This PHP has two functions that are mktime () and gmmktime () whose operation we explain below:
The role mktime (hour, minute, second, month, day, year, [adjustment-> 0 summertime and winter 1]); us back an integer value that represents the time stamp UNIX a certain date. Each of the above values can be omitted as long as they value omitted from not represent more value to its right.
The role gmmktime () works the same way that it believes that the parameters represent a GMT.
The role setlocale (category, country); allows us to establish language that will appear in the date, time, etc.. Possible categories are:
LC_TYPE Converting strings to locale
LC_NUMERIC separators numerical
To apply LC_TIME formats date and time with strftime ()
All previous LC_ALL
Validation of dates
There are many times when it is necessary to create a system to check if the date entered by the user is valid or not. This gives us two functions PHP able to perform such checks:
— Checkdate (month, day, year); Verify that the date entered is correct.
— Strtotime (cadena_fecha); Make sure the chain is correct date. This date should be in English, ie mm / dd / yy