note 97250 added to function.strftime

From: Date: Sat, 10 Apr 2010 13:58:42 +0000
Subject: note 97250 added to function.strftime
Groups: php.notes 
Request: Send a blank email to php-notes+get-168101@lists.php.net to get a copy of this message
I've just made a simple to use function that lets you get the current week number and also tells you the days that are in that week with a starting date and end date. Maybe it is helpful to somebody. Further 'finesse' can be added to this function for instance to return 1st, 2nd, 3rd, 4th not 1, 2, 3, 4 but that wasn't the main concern here. <?php # Get the number of a week in the current month or the weekday range function get_week_of_month() { # Get the current month based on the time() value $month = strftime('%m', time()); # Get the current year based on the time() value $year = strftime('%Y', time()); # Get the number of days in the current month and year $days = cal_days_in_month(CAL_GREGORIAN, $month, $year); # Get the current day in the current month and year - possible values: 1 to 31 $current_day = strftime('%e', time()); # The maximum possible weeks in the current month $weeks_in_month = ceil($days / 7); # Iterate through each day of the current month for($day=1; $day<=$days; $day++) { # and insert it into an array containig all days $days_of_month[] = $day; # Get the day number ( 0 to 6 or 1 to 7) in the current week of the current month / year # '%u' - days 1 to 7 -> 1 = Monday and 7 = Sunday # '%w' - days 0 to 6 -> 0 = Sunday and 6 = Saturday $day_of_the_week_in_a_month = strftime('%u', strtotime($month.'/'.$day.'/'.$year)); # If the current day number is equal to the maximum allowed days in a week if($day_of_the_week_in_a_month == 7) { # insert a 'breaking' point $days_of_month[] = '<endOfWeek>'; } } # Create a string list with all the values in the $days_of_month and join the elements using the ',' character $days_of_month_list = implode(',', $days_of_month); $weeks_of_month = explode(',<endOfWeek>,', $days_of_month_list); # We iterate through all possible weeks in this current month / year for($week=0; $week<$weeks_in_month; $week++) { $days_in_week[$week + 1] = explode(',', $week_of_month); } # Go through the bidimmensional array $days_in_week foreach($days_in_week as $week => $days_of_week) { # Go through each week array to see all days in that week foreach($days_of_week as $day) { # The first day of that week $day1 = $days_of_week[0]; # The last day of that week $day7 = $days_of_week[count($days_of_week) - 1]; # If the current day is a match for one of the days in one of the weeks in the current month # return the proper data about the current week number, week start day and week end day and # also the current month and year if($day == $current_day) { $data['week_no'] = $week; $data['week_day1'] = $day1; $data['week_day7'] = $day7; $data['month_year'] = strftime('%h %Y', time()); $data['week_complete'] = 'Week no. '.$week.' ('.$day1.' to '.$day7.') in '.strftime('%h %Y', time()); } } } return $data; } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 89.122.158.180 ---- Manual Page -- http://www.php.net/manual/en/function.strftime.php Edit -- https://master.php.net/note/edit/97250 Del: integrated -- https://master.php.net/note/delete/97250/integrated Del: useless -- https://master.php.net/note/delete/97250/useless Del: bad code -- https://master.php.net/note/delete/97250/bad+code Del: spam -- https://master.php.net/note/delete/97250/spam Del: non-english -- https://master.php.net/note/delete/97250/non-english Del: in docs -- https://master.php.net/note/delete/97250/in+docs Del: other reasons-- https://master.php.net/note/delete/97250 Reject -- https://master.php.net/note/reject/97250 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#168101) next »