PHP Dates and Times Finding the Day in a Week, Month, or Year - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Dates and Times Finding the Day in a Week, Month, or Year - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Sunday, May 12, 2019

PHP Dates and Times Finding the Day in a Week, Month, or Year

PHP Dates and Times




Finding the Day in a Week, Month, or Year

Problem

You want to know the day or week of the year, the day of the week, or the day of the month. For example, you want to print a special message every Monday, or on the first of every month.


Solution

Example  Finding days of the week, month, and year

             print "Today is day " . date('d') . ' of the month and ' . date('z') .
                        ' of the year.';
             print "\n";'

             $birthday = new DateTime('January 17, 1706', new DateTimeZone('America/New_York'));

             print "Benjamin Franklin was born on a " . $birthday->format('l') . ", " .
             "day " . $birthday->format('N') . " of the week.";


Discussion

The functions date() and DateTime::format() use the same format characters. Table contains all the day and week number format characters they understand.


Table   Day and week number format characters

Type    Character  Description                                                                Range                                        
Day          d           Day of the month, numeric, leading zero                  01–31
Day           j           Day of the month, numeric                                        1–31
Day          z           Day of the year, numeric                                            0–365
Day          N          Day of the week, numeric (Monday is 1)                  1–7
Day          w          Day of the week, numeric (Sunday is 0)                    0–6
Day          S           English ordinal suffix for day of the month, textual  st, th,nd, rd
Week        D          Abbreviated weekday name                                      Mon,Tue,Wed,Thu,Fri,Sat,Sun
Week        l           Full weekday name                                                     Monday,Tuesday,                                                                                                                                       WednesdayThursday, Friday,                                                                                                                     Saturday,Sunday
Week        W        ISO 8601:1988 week number in the year, numeric,
                             week 1 is the first week that has at least 4 days in
                             the current year,Monday is the first day of the week  1–53
_________________________________________________________________________________


Example   Checking for the day of the week

            if (1 == date('w')) {
                 print "Welcome to the beginning of your work week.";
            }

There are different ways to calculate week numbers and days in a week, so take care to choose the appropriate one. The ISO standard (ISO 8601) says that weeks begin on Mondays and that the days in the week are numbered 1 (Monday) through 7 (Sunday). Week 1 in a year is the first week in a year with a Thursday. This means the first week in a year is the first week with a majority of its days in that year. These week numbers range from 01 to 53.

Other week number standards range from 00 to 53, with days in a year’s week 53 potentially overlapping with days in the following year’s week 00.

As long as you’re consistent within your programs, you shouldn’t run into any trouble, but be careful when interfacing with other PHP programs or your database. For example, MySQL’s DAYOFWEEK() function treats Sunday as the first day of the week, but numbers the days 1 to 7, which is the ODBC standard. 

Its WEEKDAY() function, however, treats Monday as the first day of the week and numbers the days from 0 to 6. Its WEEK() function lets you choose whether weeks should start on Sunday or Monday, but it’s incompatible with the ISO standard.


No comments:

Post a Comment

Post Top Ad