|
In this chapter the existing functions will be studied in PHP for the employment of dates. This type of functions exists in most of computer languages and it are faced to his securing and representation in different formats.
The time in any computer language usually takes with regard to the beginning of "it was UNIX" that is January 1, 1970 to 0:00:00 AM. The simplest function that is based on this time mark is the function steal () whose returned value is the entire number that represents the mark of time corresponding to the moment in which the function was executed with regard to the age unix.
In some applications it is necessary to possess a more detailed time mark and for it we use microswindle () that returns a chain of characters with the seconds and microseconds.
In case we will want to take as a value of reference the hour of the computer from which it is executed we would use the function gettimeofday () in which we would pass for an internal parameter, sec (to know the second ones), usec (microseconds), minuteswest (nº seconds to the west of greenwich) and dstime (type of correction in daylight saving times and I hibernate).
These functions quoted previously are little used since the existence of other more finished functions, like for example the function getdate () that obtains an associative counterfoil with the information of the date and hour of the system. The elements of the above mentioned counterfoil are:
| Key |
Content |
| seconds |
I number of seconds of the current hour |
| minutes |
I number of minutes of the current hour |
| hours |
I number of hours of the current hour |
| mday |
Corresponding day of the month |
| wday |
Day of the week in numerical value (starting by 0) |
| mon |
Month of the year in numerical value. From the 1 to 12. |
| year |
Numerical value of the year |
| yday |
Day of the year in numerical value |
| weekday |
Chain of characters that it contains the day of the week (in English) |
| month |
Chain of characters that it contains the month of the year (in English) |
| 0 |
It marks of time obtained by the function getdate () |
If we do not spend any parameter to him to the function then the current hour of the system is considered and if there is received like parameter an entire number of that time it turns it to the corresponding date.
Another function to obtain the hour is the function localtime (marca_tiempo, tipo_matriz); whose values we happen to comment next:
| Index |
Key |
Content |
| 0 |
tm_sec |
I number of seconds of the stated date |
| 1 |
tm_min |
I number of minutes of the stated date |
| 2 |
tm_hour |
I number of hours of the stated date |
| 3 |
tm_mday |
Corresponding day of the month |
| 4 |
tm_wday |
Day of the week in numerical value (starting by 0) |
| 5 |
tm_mon |
Month of the year in numerical value. From 0 to 11. |
| 6 |
tm_year |
Numerical value of the year. (he turns out to be affected by the effect 2000) |
| 7 |
tm_yday |
Day of the year in numerical value |
| 8 |
tm_isdst |
It indicates if there is activated the effect of the change of hour. |
Formats of dates
The functions seen previously were allowing us to turn the entire value of the date into a value more easily understandable, although to be able to gain access to the above mentioned information it is necessary to happen for the previous step of obtaining a counterfoil. To avoid this intermediate step, PHP puts at your disposal the funcióndate (format, marca_tiempo);
This function returns us a chain of characters that corresponds with a date to which a certain format has been applied. To define the format of the date he gets ready of the following options:
| Option |
Description |
| to |
It does that in the hour the chain appears am or pm |
| TO |
It does that in the hour there appears the chain AM or PM |
| d |
Day of the month with two digits from 01 to 31 |
| D |
Day of the week as chain of three letters (in English).Ejemplo: "Mon" |
| F |
Name of the finished month as a chain of characters. Example: "March" |
| h |
It does that the hour appears in format 01 to 12 |
| H |
It does that the hour appears in format 00 to 23 |
| g |
It does that the hour appears in format 1 to 12 |
| G |
It does that the hour appears in format 0 to 23 |
| i |
It does that the minutes appear in format 00 to 59 |
| j |
It does that the day appears in format 1 to 31 |
| l (L min) |
Finished day of the week. Example: Monday |
| L |
0 writes if it is not a leap year and 1 if it it is |
| m |
It does that the month 01 appears in format to 12 |
| M |
It does that the month 1 appears in format to 12 |
| s |
It does that the second ones appear in format 00 to 59 |
| S |
Chain of characters with the ordinal suffix. Example: "th", "nd". |
| t |
Number of days of the stated month from 28 to 31 |
| OR |
Number of seconds from the beginning of "was UNIX" |
| w |
Number of the day of the week from 0 to 6 |
| AND |
Year with four numbers |
| and |
Year with two numbers |
| z |
Day of the year from 0 to 365 |
| Z |
It obtains the hourly difference in seconds with regard to the GMT |
The function strftime () represents another possibility to apply formats to a date. This function uses the local conventions of the machine from which the script is executed to return a chain with the format defined in the chosen language. His format remains defined by the following values:
| Option |
Description |
| %a |
Name of the day of the week abridged in the current language |
| %A |
Name of the finished day of the week in the current language |
| %b |
Name of the month abridged in the current language |
| %B |
Name of the finished month in the current language |
| %c |
Representation of date and hour in the current language |
| %d |
Day of the month in format 01 to 31 |
| %H |
Hour as number from 01 to 12 |
| %I |
Hour as number from 01 to 12 |
| %j |
Day of the year as number from 001 to 366 |
| %m |
Month as number from 01 to 12 |
| %M |
Minute in number |
| %p |
am or pm according to the given hour |
| %S |
Seconds in number |
| %U |
I number of the week of the year like the first Sunday like the first day of the week |
| %W |
I number of the week of the year like the first Monday like the first day of the week |
| %w |
Day of the week in number from 0 to 6 |
| %x |
Representation for defect of the date without hour |
| %X |
Representation for defect of the hour without date |
| %y |
Year in number from 00 to 99 |
| %Y |
Year in number of four numbers |
| %Z |
Name or abbreviation of the hourly area |
| %% |
Character % |
Establishing hours and dates
As soon as there was known the way of obtaining the current date, it is necessary to have a way of being able to fix a certain hour to establish for example the expiry date of a cookie, that is to say, the way of obtaining a mark of time corresponding to a certain hour.
For it PHP has two functions that are mktime () and gmmktime () whose functioning we will explain next:
The function mktime (hour, minute, second, month, day, year, [adjustment-> 0 daylight saving time and 1 winter]); he returns us an entire value that represents the time mark UNIX of a certain date. Each of the mentioned values can be omitted any time from the omitted value values are not represented any more to his right.
The function gmmktime () works of the same form what thinks that the parameters represent one hour GMT.
The function setlocale (category, country); to we allow to establish the language in those who will appear the date, hour, etc. The possible categories are:
| Option |
Description |
| LC_TYPE |
Chains conversion to regional configuration |
| LC_NUMERIC |
Numerical dividers |
| LC_TIME |
To apply formats of date and hour with strftime () |
| LC_ALL |
All the previous ones |
Ratification of dates
There exist numerous occasions in which the creation of a system is necessary to verify if the date introduced by the user is cost or not. For it PHP offers to us two functions capable of realizing the above mentioned cross-check:
- checkdate (month, day, year); He verifies that the introduced date is correct.
- strtotime (cadena_fecha); He verifies that the chain of date is correct. For it the date must be in English format, that is to say, mm/dd/aa
|