Daniel Bejan
Daniel Bejan

Reputation: 1468

Calculate number of each weekday in the month

I'm working as a deliverer at a farm and have some sort of notebook(not laptop) in which I have everything noted like how many liters of milk I get to someone, when, and how much they have to pay at the end of the month, and I want to make some kind of application to keep track of everything and throw the notebook away. I'm using HTML, PHP and MySQL for this and this is what I've got till now :

This is the form:
 <html>
<body>

<form action="insert.php" method="post">
<table border="0"><tr>
<td>Name: </td><td><input type="text" name="name"></td>
</tr><tr>
<td>Multiplier:</td><td><input type="text" name="multiplier"/></td></tr><tr>
<td>Sunday</td><td><input type="checkbox" name="values[]" value="1" /></td></tr><tr>
<td>Monday</td><td><input type="checkbox" name="values[]" value="2" /> </td></tr><tr>
<td>Tuesday</td><td><input type="checkbox" name="values[]" value="3" /></td></tr><tr>
<td>Wednesday</td><td><input type="checkbox" name="values[]" value="4" /></td></tr><tr>
<td>Thursday</td><td><input type="checkbox" name="values[]" value="5" /></td></tr><tr>
<td>Friday</td><td><input type="checkbox" name="values[]" value="6" /></td></tr><tr>
<td>Saturday</td><td><input type="checkbox" name="values[]" value="7" /></td></tr><br>
<td><input type="submit" value="Add"></td></tr>
</form>

</body>
</html>

*The Multiplier is how many liters I get to that person every time(usually the amount is the same every time).

I'm using this to add new people to the database with the help of this PHP script:

<?php
$con=mysqli_connect("","root","","Lapte");
//Check if connected
if(mysqli_connect_errno()){
    echo "Failed to connect to database: ".mysqli_connect_errno();
}
$name=$_POST['name'];
$days=sizeof($_POST['values']);
$multiplier=$_POST['multiplier'];
$days=$days*$multiplier;
$totalPerWeek=$days*2.5;


$sql="INSERT INTO deliveries (Name, Times/Week, Pay)
VALUES
('$name','$days','$totalPerWeek')";

if(!mysqli_query($con,$sql)){
    die('Error: '.mysqli_error($con));
}
    echo "$name added to database.";
mysqli_close($con);
?>

I'm using EasyPHP for a local server and have my database already created.

Now my dilemma is: If for example I have to get milk to mr. X on Sunday and Thursday, and this month there are 5 Thursdays and only 4 Sundays I can't just multiply by 4 the result I get per week.How can I deal with this issue?

Upvotes: 1

Views: 817

Answers (1)

Philwn
Philwn

Reputation: 330

Using the below function supply the month and year and an array of each weekday will be returned with a value representing the amount in that month.

        <?php
            function calculateDays($month, $year) {
                //how many days in given month
                $TotalDays = cal_days_in_month(CAL_GREGORIAN, $month, $year);

                $DayCount = array();

                for ($i = 1; $i <= $TotalDays; $i++) {
                    //for each day (1st to 30th/31st) get the weekday
                $DayOfWeek = date('l', mktime(0, 0, 0, $month, $i, $year));
                //add 1 to the counter
                $DayCount[$DayOfWeek]++;
                }
                return $DayCount;
            }
    ?>

Alternatively using the below function supply the day aswell as month and year and it will return a numerically value of how many times given day appears in that month.

      <?php
        function countDayinMonth($day, $month, $year) {
            //how many days in given month
            $TotalDays = cal_days_in_month(CAL_GREGORIAN, $month, $year);

            $Count = 1;

            for ($i = 1; $i <= $TotalDays; $i++) {
            if ($day == date('l', mktime(0, 0, 0, $month, $i, $year)))
            //add 1 to the counter
            $Count++;
            }
            return $Count;
        }
?>

UPDATE: The following function will just return a numerical value for the current day/month/year.

So today being Thursday 1st August will return 5

        function countDayinMonth() {
        $day = date('l');
        $month = date('n');
        $year = date('Y');
        //how many days in given month
        $TotalDays = cal_days_in_month(CAL_GREGORIAN, $month, $year);

        $Count = 0;

        for ($i = 1; $i <= $TotalDays; $i++) {
        if ($day == date('l', mktime(0, 0, 0, $month, $i, $year)))
        //add 1 to the counter
        $Count++;
        }
        return $Count;
    }

    echo countDayinMonth();

Upvotes: 2

Related Questions