Solved

Trying to determine how many 'selling' days left in a month

Posted on 2013-10-26
7
348 Views
Last Modified: 2013-10-26
Hi,
Our business isn't open on Sundays. In my app I need to show how many selling days are left in the month (days we are open). Currently you can show how many days left in a month using something  like this:
<?php echo date('t') - date('j');?>

But how could I take into account Sundays?
0
Comment
Question by:tjyoung
[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
  • 4
  • 3
7 Comments
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 39602528
Here you go:

<?php
$start = new DateTime(); //get today
$end = new DateTime('last day of this month'); //get the end of the month
$days = $start->diff($end, true)->days; //number of days between now and the end of the month
$sundays = intval($days / 7) + ($start->format('N') + $days % 7 >= 7); //number of sundays

$sellingDays = $days - $sundays; //number of days minus the Sundays

printf("The are %d selling days left this month", $sellingDays);
?>

Open in new window

0
 
LVL 1

Author Comment

by:tjyoung
ID: 39602623
Sorry, I don't suppose you know how to implement the above in php5.2?
'last day of this month' for example doesn't work unless its 5.3 and above and I can't upgrade the machine unfortunately....
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39602648
Not really. Don't have access to 5.2 any more!

The diff() method is also >=5.3.

You really ought to upgrade your system though - for security reasons if nothing else. If this is a hosted platform, then chase them up (or change providers)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39602652
Hmmm. Thought a little more and maybe try this. We don't actually need the date of the end of the month, just the day number, which you can get with format('t'):

$start = new DateTime();
$days = $start->format('t') - $start->format('d');
$sundays = intval($days / 7) + ($start->format('N') + $days % 7 >= 7);

$sellingDays = $days - $sundays;
printf("The are %d selling days left this month", $sellingDays);

Open in new window

0
 
LVL 1

Author Comment

by:tjyoung
ID: 39602654
Great trying it now...
0
 
LVL 1

Author Comment

by:tjyoung
ID: 39602662
Awesome, that did the trick! I appreciate you taking another look Chris.
Thanks again,
Todd
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39602670
No worries ;)
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

728 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