| | |||||||
| |||||||
| |||||||
|
|
|
|
|
|
|
More Time Manipulation with PHP(Page 1 of 2 ) In this conclusion to an eight-part article series on manipulating date and time in programs with PHP, you'll learn how to use the getWeekday() method, the getDayOfYear() method, and many more. This article is excerpted from chapter 12 of the book Beginning PHP and PostgreSQL 8: From Novice to Professional, written by W. Jason Gilmore and Robert H. Treat (Apress; ISBN: 1590595475). More Manipulation Methods getWeekday() integer getWeekday() ThegetWeekday()method returns the numerical offset of the day specified by the date object. An example follows: $date = new Date(); This returns the following, which is a Sunday, because Sunday’s numerical offset is 7: --------------------------------------------7 setToWeekday() boolean setToWeekday (int weekday, int n [, int month [, int year]]) ThesetToWeekday()method sets the date to thenthweekdayof themonthandyear, returningTRUEon success andFALSEotherwise. If nomonthandyearare provided, the present month and year are used. As of the time of writing, this method was broken; quite likely it will have been fixed by the time this book is published. getDayOfYear() integer getDayOfYear() ThegetDayOfYear()method returns the numerical offset of the day specified by the date object. An example follows: $date = new Date(); The following is the result: --------------------------------------------186 getWeekOfYear() integer getWeekOfYear() ThegetDayOfYear()method returns the numerical offset of the week specified by the date object: $date = new Date(); This returns: --------------------------------------------27 getISOWeekOfYear() integer getISOWeekOfYear() ThegetISOWeekOfYear()method returns the week number of the date represented by the date object according to the ISO 8601 specification. ISO 8601 states that the first week of the year is the week containing the first Thursday. For instance, the first day of 2005 fell on a Sunday, but January 2 through 8 contained the first Thursday; therefore, January 1 does not even count as falling in the first week of the year. You might think this a tad odd; however, the decision is almost arbitrary in that it just standardizes the method for determining what constitutes the year’s first week. Let’s see this explanation in action by querying for the week number in which January 4 falls: $date = new Date(); The following is returned: --------------------------------------------1 So, given that January 1 doesn’t qualify as falling within the first week of the year, within what week does it fall? You might be surprised to learn the ISO standard actually considers it to be the 53rd week of 2004: $date = new Date(); This returns: --------------------------------------------53 setToLastMonthDay() boolean setToLastMonthDay() ThesetToLastMonthDay()method adjusts the date object’sday attribute to the last day of the month specified by themonth attribute, returningTRUE on success andFALSEotherwise. An example follows: $date = new Date(); The following output is returned: --------------------------------------------30 setFirstDow() boolean setFirstDow() ThesetFirstDow()method sets the date object’sdayattribute to the first day of the week as specified by theweekstartattribute, returningTRUEon success andFALSEotherwise. By default,weekstartis set to Monday. The following example sets the date April 28, 2006 (which is a Friday), and then moves the date to the first day of the week (a Monday): $date = new Date(); This returns: --------------------------------------------Array ( setLastDow() boolean setLastDow() ThesetLastDow()method sets the date object’sday attribute to the last day of the week, returningTRUEon success andFALSEotherwise. This day is dependent upon the value of theweekstart attribute, which is set to Monday by default. The following example sets the date April 28, 2006 (which is a Friday), and then moves the date to the last day of the week (a Sunday): $date = new Date(); This returns: --------------------------------------------Array ( More Programming Basics Articles |
| |
| |