1

I want to get number of weekends and number of bussiness day by I know only $startDate and $endDate, Is have any PHP's function that can calculate automatic ?

This is my code :

 $endDate = strtotime($endDate);
 $startDate = strtotime($startDate);
 echo $days = ($endDate - $startDate) / 86400 + 1; 

My code will return how many day, and how I need more is what day ? How I can get it?

Example:

startDate:`2014-11-17`

endDate:`2014-11-19`

I want this Output:

it's 3 day
2014-11-17 is Monday
2014-11-18 is Tueday
2014-11-19 is Wendnesday
Rizier123
  • 58,877
  • 16
  • 101
  • 156
koe
  • 736
  • 1
  • 12
  • 33

5 Answers5

1

How about...

for ($time = $startDate; $time <= $endDate; $time += 86400) {
    echo date('Y-m-d \i\s l', $time) . '<br>';
}
rjdown
  • 9,162
  • 3
  • 32
  • 45
0

This might help you, or at least may give you some clue

$endDate = strtotime($endDate);
$startDate = strtotime($startDate);
echo $days = ($endDate - $startDate) / 86400 + 1;
$time = $startDate;
while ($time <= $endDate) {
   echo date('Y-m-d', $time) . ' is ' . date('l', $time);
   $time += 86400;
}
if ($time != $endDate) {
   echo date('Y-m-d', $time) . ' is ' . date('l', $time);
}
Javad
  • 4,339
  • 3
  • 21
  • 36
0

This should work for you:

<?php

    $startDate = strtotime("2014-11-17");
    $endDate = strtotime("2014-11-19");

    echo "It's " . $days = ($endDate - $startDate) / 86400 + 1 . " days";

    for($count = 0; $count < $days; $count++)
        echo "<br />" . date('Y-m-d', strtotime('+' . $count . ' day', $startDate)) . ' is ' . date('l', strtotime('+' . $count . ' day', $startDate));

?>

Output:

It's 3 days
2014-11-17 is Monday
2014-11-18 is Tuesday
2014-11-19 is Wednesday
Rizier123
  • 58,877
  • 16
  • 101
  • 156
0

There's no built-in. Excel has NETWORKDAYS(), but in PHP you must roll your own.

There is a PHP solution at Day difference without weekends Note its limitations in the comments there e.g. if a public holiday falls on a weekend day.

The following page describes an alternative implementation of NETWORKDAYS(): http://www.cpearson.com/excel/betternetworkdays.aspx It's not PHP but it demonstrates the logic.

Unfortunately there is no shortcut other than looping through each day in the period and deciding whether or not to count it. You need to accept arguments for (or hard-code) the weekend days and the dates of any public holidays (if you are excluding public holidays).

If you are doing this frequently and for long periods, you might pre-compute and cache the number of business days for each calendar month and year; then, at run-time, you look up the number of days in each whole year within the period, then the remaining whole months, then do the ordinary loop for the remaining days at the start and end of the period.

EDIT: If you just want to exclude weekends (not public holidays), then you can calculate 5 days for each whole week in the period, and then calculate any remaining days: https://github.com/bgarlock/scripts/blob/master/PHP%20Equivalent%20of%20MS%20Excel%20NETWORKDAYS%20function.php

Community
  • 1
  • 1
-1

Please find below code I am sure it works for you

NOTE : Parameter passed P1D is denoting 1 Day difference same way you can pass P1M for 1 month P1W for 1 week and P1Y for 1 Year.

$date1 = '29/08/2013';
$date2 = '03/09/2013';

function returnDates($fromdate, $todate) {
    $fromdate = \DateTime::createFromFormat('d/m/Y', $fromdate);
    $todate = \DateTime::createFromFormat('d/m/Y', $todate);
    return new \DatePeriod(
    $fromdate,
    new \DateInterval('P1D'),
    $todate->modify('+1 day')
);
}

$datePeriod = returnDates($date1, $date2);
foreach($datePeriod as $date) {
    echo $date->format('d/m/Y'), PHP_EOL; //you can set any date format here
}
Lalit Sharma
  • 555
  • 3
  • 12