yeahgd
yeahgd

Reputation: 58

Is there a function to calculate weekdays from today PHP

I'm writing a code which need to calculate the number of weekdays from a date to today. I just have today's date and with that i want to pass a number to the function so it can return me back the number of weekdays since x days.

e.g :

function getWorkingDays($number){
    // code...
    return $value;
}

// if we are monday
getWorkingDays(2) // return Thursday's date

I got this problem since two days now and i'm getting very boring, hope someone got a solution.

Upvotes: 1

Views: 40

Answers (1)

jspit
jspit

Reputation: 7703

This is very easy to do with the DateTime object:

$date = date_create('2020-01-06'); //a Monday 
$numberWeekdays = 2;
$date->modify('-'.$numberWeekdays.' weekdays');

echo $date->format('l Y-m-d');
//Thursday 2020-01-02

If you need Today as the basis , you can also use date_create('Today').

Upvotes: 1

Related Questions