Solved

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

Posted on 2013-10-26
7
315 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 42

Accepted Solution

by:
Chris Stanyon earned 500 total points
Comment Utility
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
Comment Utility
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 42

Expert Comment

by:Chris Stanyon
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 42

Expert Comment

by:Chris Stanyon
Comment Utility
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
Comment Utility
Great trying it now...
0
 
LVL 1

Author Comment

by:tjyoung
Comment Utility
Awesome, that did the trick! I appreciate you taking another look Chris.
Thanks again,
Todd
0
 
LVL 42

Expert Comment

by:Chris Stanyon
Comment Utility
No worries ;)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now