Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP date - Unix timestamp

Could somebody please help me to resolve this PHP timestamp riddle

I'm trying to find number of weekdays based on two given dates.

I'm getting a string date eg starting date: 01/10/2019 (1st of October 2019) and explode it to a array => $s

Array $s set like this:

Array ( [0] => 01 [1] => 10 [2] => 2019 )

then I made a Unix timestamp from it :

date('U', mktime(0, 0, 0, $s[1], $s[0], $s[2]));

I get 1569884400 as the result of above date conversion.

then I use a loop to run this and so I can find number of week days based on the 2 given dates..

this is a result of that loop and as you can see for some reason 27th October repeating.

Just to clarify the top of this PHP page i have timezone set

date_default_timezone_set('Europe/London');

this is the code behind the below result

total $total_days based on last date (in this case 31st October 2019 and ie: 31 days)

October 2019 should have 23 working days, Since I get 2x 27th Octobers I will get only 22 weekdays.

What am I doing wrong here?

 $wk_days = array(1, 2, 3, 4, 5);
  $days = 0;
  for ($i = 0; $i < $total_days; $i++) {
    $tmp = $first + ($i * 86400);
    echo  $i." x86400 add to ". $first . " ==> " . $tmp. " and the new date is "  .date('Y-m-d',$tmp) ."<br/>";

    $chk = date('w', $tmp);

    if (in_array($chk, $wk_days)) {
      $days++;
    }
  }

0 x86400 add to 1569884400 ==> 1569884400 and the new date is 2019-10-01

1 x86400 add to 1569884400 ==> 1569970800 and the new date is 2019-10-02

2 x86400 add to 1569884400 ==> 1570057200 and the new date is 2019-10-03

3 x86400 add to 1569884400 ==> 1570143600 and the new date is 2019-10-04

4 x86400 add to 1569884400 ==> 1570230000 and the new date is 2019-10-05

...

23 x86400 add to 1569884400 ==> 1571871600 and the new date is 2019-10-24

24 x86400 add to 1569884400 ==> 1571958000 and the new date is 2019-10-25

25 x86400 add to 1569884400 ==> 1572044400 and the new date is 2019-10-26

26 x86400 add to 1569884400 ==> 1572130800 and the new date is 2019-10-27 //***

27 x86400 add to 1569884400 ==> 1572217200 and the new date is 2019-10-27 //***

28 x86400 add to 1569884400 ==> 1572303600 and the new date is 2019-10-28

29 x86400 add to 1569884400 ==> 1572390000 and the new date is 2019-10-29

30 x86400 add to 1569884400 ==> 1572476400 and the new date is 2019-10-30

like image 767
sam6640621 Avatar asked Sep 18 '26 20:09

sam6640621


2 Answers

You don't need anything that complicated. You can have a simple while loop and count the weekdays. It might not be the most efficient way to do it, but it should work.

function getNoOfWeekdays(string $startdate, string $enddate, string $format = 'd/m/Y'): int
{
    $start = date_create_from_format($format, $startdate);
    $end = date_create_from_format($format, $enddate);

    $count = 0;
    while ($start <= $end) {
        if ($start->format('N') < 6) $count++;
        $start->modify('+1 days');
    }

    return $count;
}
like image 103
Dharman Avatar answered Sep 21 '26 09:09

Dharman


Maybe you could try with this code:

  $initial = "2019-10-01";
  $last = "2019-10-31";
  $wk_days = array(1, 2, 3, 4, 5);
  $days = 0;
  $date = $initial;
  do{

    $chk = date("w",strtotime($date));
    if(in_array($chk,$wk_days))
      $days++;
    $date = date("Y-m-d",strtotime("$date +1 day"));

  }while($date!=date("Y-m-d",strtotime("$last +1 day")));
    echo $days;
?>
like image 43
Matías Rodríguez Avatar answered Sep 21 '26 09:09

Matías Rodríguez