?
Solved

Total days in a month

Posted on 2016-10-13
12
Medium Priority
?
55 Views
Last Modified: 2016-10-13
Hi,

I need to count the number of times a certain day exists in a month, so how many mondays in this month etc

Tried this but didnt count correctly, kept saying there were 5 tuesday this monday when there are only 4.

<?php $now=date('m');
if (($dow = date('w', $now)) == $row['day_of_the_week']) $dow = 7; 
$begin = $now - (86400 * ($dow));

echo "x ".ceil(date('d', $begin) / 7); ?>

Open in new window


Thanks Simon
0
Comment
Question by:pc-buddy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +2
12 Comments
 
LVL 9

Accepted Solution

by:
Evan Cutler earned 2000 total points
ID: 41842733
you could try this:

$Monday=0;
       for($i=1; $i<=date("t", strtotime("2012-09-01")); $i++) {
           if(date("l", strtotime("2012-09-".$i)) === 'Monday') {
               $Monday++;
           }     
       }
       echo "no of Monday=".$Monday;

Open in new window

0
 

Author Comment

by:pc-buddy
ID: 41842734
Thanks Evan, that says 4 :) we should see 5 ?

Thanks
0
 

Author Comment

by:pc-buddy
ID: 41842738
ignore me, i changed the dat to this month :)

Thanks
0
WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

 
LVL 9

Expert Comment

by:Evan Cutler
ID: 41842750
You good then?
0
 

Author Comment

by:pc-buddy
ID: 41842753
Nearly, what parts do I change to make it show tuesdays ? :)

Thanks
0
 
LVL 9

Expert Comment

by:Evan Cutler
ID: 41842774
So $Monday is a variable.  your option to change or not. or make it agnostic (ie. $day)

In line 3 you'll see
=== 'Monday'

Open in new window


That...you change to 'Tuesday'
0
 

Author Closing Comment

by:pc-buddy
ID: 41842778
Thank for all your help :)
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 41842779
A code sample is available in this article.  Please see Practical Application #5:
https://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL-Procedural-Version.html
1
 

Author Comment

by:pc-buddy
ID: 41842780
Propbaly the dumbest question I've ever ask :)

TUESDAY

Thanks
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 41842789
Here's a generalized solution that works over leap year and daylight savings time.
https://iconoun.com/demo/find_weekdays.php
<?php // demo/find_weekdays.php
/**
 * https://www.experts-exchange.com/questions/28976308/Total-days-in-a-month.html
 *
 * https://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL-Procedural-Version.html
 * https://www.experts-exchange.com/articles/20920/Handling-Time-and-Date-in-PHP-and-MySQL-OOP-Version.html
 */
error_reporting(E_ALL);

function find_weekdays($date='Today', $day='Sunday')
{
    $timestamp   = strtotime(date('Y-m-0', strtotime($date)));
    $weekdays[0] = date('Y-m-d D', strtotime("first  $day", $timestamp));
    $weekdays[1] = date('Y-m-d D', strtotime("second $day", $timestamp));
    $weekdays[2] = date('Y-m-d D', strtotime("third  $day", $timestamp));
    $weekdays[3] = date('Y-m-d D', strtotime("fourth $day", $timestamp));
    $weekdays[4] = date('Y-m-d D', strtotime("fifth  $day", $timestamp));
    if (substr($weekdays[4],5,2) != date('m', strtotime("first $day", $timestamp))) unset($weekdays[4]);
    return $weekdays;
}

// USE THE FUNCTION
$mon = 'February 2008';
$day = 'Friday';
$res = find_weekdays($mon, $day);
echo PHP_EOL . "IN $mon, $day OCCURS " .  count($res) . " TIMES";
echo PHP_EOL;
print_r($res);

Open in new window

1
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 41842841
I know this has been answered already, but I'd recommend you reconsider using the code you've selected, as it's VERY heavy for such a simple problem.

Both date and strtotime functions are called a whopping 61 times EACH in the accepted code.

Take the following function, which runs a whopping 1600% FASTER than the original code, simply by using some basic math:

// Day numbers: 0 = Sunday, 1 = Monday, ...., 6 = Saturday
function number_days_in_month($day_number = 0, $month = "2016-10-01")
{
  $ts = strtotime($month);

  $days_in_month = date("t",$ts);
  // echo "There are {$days_in_month} total days in {$month}.\n";

  $starting_day = date("w",$ts); // 0 = Sunday, 6 = Saturday
  // echo "The day of the week for {$month} is {$starting_day}.\n";
  
  $first_day_offset = $day_number - $starting_day;
  // echo "The first day offset is {$first_day_offset}\n";
  
  $days_left_in_month = $days_in_month - ($first_day_offset < 0 ? 7 + $first_day_offset : $first_day_offset);
  // echo "After taking the offset into consideration, there are {$days_left_in_month} days left in the month.\n";

  return ceil($days_left_in_month/7);
}

Open in new window


Note that I've left in some commented echo statements so that you can follow along with what the code is doing if you want to try it out a few times.

To be clear, the speed differences we're talking about are milliseconds vs. microseconds. The original function you accepted takes nearly 6 milliseconds to run. That's a pretty long time for such a simple problem, and if you run it more than once, it can really add up quickly.

Choosing not to care about milliseconds or efficiency in smaller functions like these is how companies end up with larger applications that run slow - because there are lots of small inefficiencies that never get fixed and then it's too late to go back and fix them later (you'll never get someone to later agree to spending money and time to speed the application up by 6 milliseconds), so being efficient at the beginning is important for the long run.
1
 
LVL 58

Expert Comment

by:Julian Hansen
ID: 41843109
Alternative for interest
function daysInMonth($date, $dow) 
{
	$daysinmonth = date('t', $date);
	$firstdayofmonth = date('w', $date);
	$fivedays = $daysinmonth - 28;
	$dow += $dow < $firstdayofmonth ? 7 : 0;

	return $dow >= $firstdayofmonth && $dow < $firstdayofmonth + $fivedays ? 5  : 4;
}

$date = strtotime('2016-02-01');
$days = daysInMonth($date, SUNDAY);
echo "#days = {$days}<br/>";
$days = daysInMonth($date, MONDAY);
echo "#days = {$days}<br/>";
$days = daysInMonth($date, TUESDAY);
echo "#days = {$days}<br/>";

Open in new window

1

Featured Post

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question