Work with dates
Often you have to see how a real circus begins when a PHP programmer comes up with dates. Here, there are self-written magical functions that calculate something there and constantly stumble at the border of months or years, or on changing the clock to winter / summer time, here also the mysterious use of built-in functions, with tambourines and dances.
But it is necessary, in fact, almost always quite a bit.
Under the cut - a couple of useful, in my opinion, tips for novice programmers.
Everything is simple: we give this function a text description of the date, we get a timestamp at the output. The hints here are (this is, in general, for those who are too lazy to follow the link):
1. using relative date descriptions (for example: "-1month" - current date minus 1 month, "+ 1year + 1month" - current date plus 1 year and 1 month)
2. the use of the second optional parameter $ now is the timestamp relative to which the date is calculated. For example, print the date - Friday of the following week:
Why was it impossible to use simple strtotime ("next friday") here ? Yes, because if today, say, Thursday, then strtotime (“next friday”) will return us the timestamp of the next Friday - that is, Friday of this week, but this is not what we need (though we could use strtotime (“ next week friday ") , but shh).
Another example. We calculate the date - the first Monday of the next month.
This function returns a date formatted taking into account the current locale in the specified format. Often this can help, for example, refuse to use arrays with the names of the months - in favor of the built-in features of the language.
Happy programming!
But it is necessary, in fact, almost always quite a bit.
Under the cut - a couple of useful, in my opinion, tips for novice programmers.
Number of times: strtotime
Everything is simple: we give this function a text description of the date, we get a timestamp at the output. The hints here are (this is, in general, for those who are too lazy to follow the link):
1. using relative date descriptions (for example: "-1month" - current date minus 1 month, "+ 1year + 1month" - current date plus 1 year and 1 month)
2. the use of the second optional parameter $ now is the timestamp relative to which the date is calculated. For example, print the date - Friday of the following week:
$ now = strtotime ("this sunday"); print strftime ("% c", strtotime ("next friday", $ now));
Why was it impossible to use simple strtotime ("next friday") here ? Yes, because if today, say, Thursday, then strtotime (“next friday”) will return us the timestamp of the next Friday - that is, Friday of this week, but this is not what we need (though we could use strtotime (“ next week friday ") , but shh).
Another example. We calculate the date - the first Monday of the next month.
$ next_month_fisrt_day = date ("Ym-01", strtotime ("next month")); $ next_month_fisrt_monday_ts = strtotime ("this monday", strtotime ($ next_month_fisrt_day)); print strftime ("% c", $ next_month_fisrt_monday_ts). "\ n";
Number two: strftime
This function returns a date formatted taking into account the current locale in the specified format. Often this can help, for example, refuse to use arrays with the names of the months - in favor of the built-in features of the language.
Happy programming!