Solved

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

Posted on 2013-10-26
7
342 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
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

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!

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

730 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