PHP script count leave days + Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
John031
Forum Newbie
Posts: 4
Joined: Thu Jul 21, 2016 11:04 pm

PHP script count leave days + Problem

Post by John031 »

Dear developer!
I found a script on the internet, by this script i can count leave days Like (2016-07-15) to (2016-07-22) without weekend days.
But i want count multiple records like One employee in January take leave from date of 2016-01-01 To 2016-01-05 and i record this leave in MySQL and this employee take leave in next month from 2016-02-10 To 2016-02-20 and i want count days of this two records in this scripts.

Code: Select all

<?php
$start = new DateTime('2016-07-15');
$end = new DateTime('2016-07-22');
// otherwise the  end date is excluded (bug?)
$end->modify('+1 day');
$interval = $end->diff($start);
// total days
$days = $interval->days;
// create an iterateable period of date (P1D equates to 1 day)
$period = new DatePeriod($start, new DateInterval('P1D'), $end);
// best stored as array, so you can add more than one
$holidays = array('2016-07-22','2016-07-21');
foreach($period as $dt) {
    $curr = $dt->format('D');
    // for the updated question
    if (in_array($dt->format('Y-m-d'), $holidays)) {
       $days--;
    }
    // substract if Saturday or Sunday
    if ($curr == 'Sat' || $curr == 'Sun') {
        $days--;
    }
}
echo $days; // 4
?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP script count leave days + Problem

Post by requinix »

So run the code twice for the two date ranges.

You should probably move the code into a function which takes $start and $end as arguments and returns $days.
John031
Forum Newbie
Posts: 4
Joined: Thu Jul 21, 2016 11:04 pm

Re: PHP script count leave days + Problem

Post by John031 »

Thank you requinix, But i'm not professional in PHP, you can help me fix this script for me?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP script count leave days + Problem

Post by requinix »

Do it for you? No. But I'll help you do it. Because if you're going to work with PHP code then you need to know some PHP.

The only information that code needs is the $start and $end dates, and the only information it "returns" is the number of $days. So putting that code into a function should look like

Code: Select all

function countWorkDays(DateTime $start, DateTime $end) {
	// otherwise the  end date is excluded (bug?)
	$end = clone $end;
	$end->modify('+1 day');

	// ...

	return $days;
}
(the clone bit is important but not something to worry about)

Then you call the function like

Code: Select all

echo countWorkDays(new DateTime('2016-07-15'), new DateTime('2016-07-22'));
John031
Forum Newbie
Posts: 4
Joined: Thu Jul 21, 2016 11:04 pm

Re: PHP script count leave days + Problem

Post by John031 »

Thank you for helpful reply dear requinix, But i so tried i receive a lot of errors, I need this code so emergency i hope help me. Thanks
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP script count leave days + Problem

Post by requinix »

Then you best start learning now.

What errors? What's the rest of your code?
John031
Forum Newbie
Posts: 4
Joined: Thu Jul 21, 2016 11:04 pm

Re: PHP script count leave days + Problem

Post by John031 »

Look to this script i just added your two line to my script. I'm really confusing right now i don't know how i can fix this. please assist me.

Code: Select all

<?php

$start = new DateTime('2016-07-15');
$end = new DateTime('2016-07-22');

// otherwise the  end date is excluded (bug?)


function countWorkDays(DateTime $start, DateTime $end) {
        // otherwise the  end date is excluded (bug?)
        $end = clone $end;
        $end->modify('+1 day');

        // ...

        return $days;
}

echo countWorkDays(new DateTime('2016-07-15'), new DateTime('2016-07-22'));

$interval = $end->diff($start);

// total days
$days = $interval->days;

// create an iterateable period of date (P1D equates to 1 day)
$period = new DatePeriod($start, new DateInterval('P1D'), $end);

// best stored as array, so you can add more than one
$holidays = array('2016-07-22','2016-07-21');


foreach($period as $dt) {
    $curr = $dt->format('D');

    // for the updated question
    if (in_array($dt->format('Y-m-d'), $holidays)) {
       $days--;
    }

    // substract if Saturday or Sunday
    if ($curr == 'Sat' || $curr == 'Sun') {
        $days--;
    }
}


echo $days; // 4

?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP script count leave days + Problem

Post by requinix »

So in general, not everything on the internet is supposed to be copied and pasted into your own code. You're supposed to read and understand what people are saying, then adapt it to fit into your requirements.

Given that I suggested to move "that code" into a function, does

Code: Select all

function countWorkDays(DateTime $start, DateTime $end) {
        // otherwise the  end date is excluded (bug?)
        $end = clone $end;
        $end->modify('+1 day');

        // ...

        return $days;
}
make sense? Doesn't it seem rather odd for the entire function to just be those lines? Doesn't that "..." look important?

But you know what? I'm busy doing other things I would rather do than explain how to learn about coding from other people on the internet. So here.

Code: Select all

<?php

function countWorkDays(DateTime $start, DateTime $end) {
        // otherwise the  end date is excluded (bug?)
        $end = clone $end;
        $end->modify('+1 day');

	$interval = $end->diff($start);

	// total days
	$days = $interval->days;

	// create an iterateable period of date (P1D equates to 1 day)
	$period = new DatePeriod($start, new DateInterval('P1D'), $end);

	// best stored as array, so you can add more than one
	$holidays = array('2016-07-22','2016-07-21');


	foreach($period as $dt) {
	    $curr = $dt->format('D');

	    // for the updated question
	    if (in_array($dt->format('Y-m-d'), $holidays)) {
	       $days--;
	    }

	    // substract if Saturday or Sunday
	    if ($curr == 'Sat' || $curr == 'Sun') {
	        $days--;
	    }
	}

        return $days;
}

echo countWorkDays(new DateTime('2016-07-15'), new DateTime('2016-07-22'));
Post Reply