Solved

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

Posted on 2013-10-26
7
341 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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.  …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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 a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

809 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