Jithin C V
Jithin C V

Reputation: 13

Generate Date with Day (1-7/Mon-Sun), Week Of Month (1-5), Month (1-12) in PHP

I have below Data in DataBase,

Day Number   --> 1 to 7 (Representing Monday to Sunday)

Week Number  --> 1 to 5 (Representing Week Number of Month)

Month Number --> 1 to 12 (Representing Jan to Dec)

Year         --> Can be this year future year

In PHP I need to generate corresponding date (YYYY-MM-DD) with above data.

Upvotes: 0

Views: 426

Answers (2)

Jithin C V
Jithin C V

Reputation: 13

I have written below functions for achieving it. And it working fine with initial tests.

$Day_Config = array(
    1 => 'Monday',
    2 => 'Tuesday',
    3 => 'Wednesday',
    4 => 'Thursday',
    5 => 'Friday',
    6 => 'Saturday',
    7 => 'Sunday'
  );

$Week_Config = array(
      1 => 'first',
      2 => 'second',
      3 => 'third',
      4 => 'fourth',
      5 => 'fifth',
    );

  function weekOfMonth($date) {
    #Get the first day of the month.
    $firstOfMonth = strtotime(date("Y-m-01", strtotime($date)));

    #Apply formula.
    return intval(date("W", strtotime($date))) - intval(date("W", $firstOfMonth)) + 1;
  }
  
  function getDateFromText ($year,$month,$week,$day) {
    global $Day_Config;
    global $Week_Config;
    
    $txtFormat  = $Week_Config["$week"]." ".strtolower($Day_Config["$day"])." of ".date("F", mktime(0, 0, 0, $month, 10))." ".$year;
    $DateFromTtxt       = date('Y-m-d', strtotime("$txtFormat"));

    return $DateFromTtxt;
  }

function getNextRuntime ($day,$week,$month,$time) {
    #Get Current Day Details
    $Year       = date("Y");
    $thisMonth  = date("n");
    $thisWeek   = weekOfMonth(date("Y-m-d"));
    $thisDay    = date('N');

    #Get Received Data From DB
    $Days       = explode(",",$day);
    $Weeks      = explode(",",$week);
    $Months     = explode(",",$month);

    #Loop Through Months
    foreach ($Months as $Value_Month){
      if($Value_Month >= $thisMonth){
        #Loop Through Weeks
        foreach ($Weeks as $Value_Week){
          if($Value_Month == $thisMonth){
            if($Value_Week >= $thisWeek){
              #Loop Through Days
              foreach ($Days as $Value_Day){
                if($Value_Month == $thisMonth && $Value_Week == $thisWeek){
                  if($Value_Day >= $thisDay){
                    if($Value_Day == $thisDay && $time > date('H:i:00')){
                      return getDateFromText($Year, $Value_Month, $Value_Week, $Value_Day);
                    } elseif ($Value_Day != $thisDay) {
                      $Result       = getDateFromText($Year, $Value_Month, $Value_Week, $Value_Day);
                      $Result_Month = date('n', strtotime($Result));
                      if($Result_Month == $Value_Month){
                        return $Result;
                      }
                    }
                  }
                } else {
                  $Result       = getDateFromText($Year, $Value_Month, $Value_Week, $Value_Day);
                  $Result_Month = date('n', strtotime($Result));
                  if($Result_Month == $Value_Month){
                    return $Result;
                  } else {
                    break;
                  }
                }
              }  
            }
          } else {
            foreach ($Days as $Value_Day){
              $Result       = getDateFromText($Year, $Value_Month, $Value_Week, $Value_Day);
              $Result_Month = date('n', strtotime($Result));
              if($Result_Month == $Value_Month){
                return $Result;
              } else {
                break;
              }
            }
          }
        }
      }
    }

    #Select Next Year If Current Year Not Provided Any Date
    ++$Year;
    foreach ($Months as $Value_Month){
      foreach ($Weeks as $Value_Week){
        foreach ($Days as $Value_Day){
          $Result       = getDateFromText($Year, $Value_Month, $Value_Week, $Value_Day);
          $Result_Month = date('n', strtotime($Result));
          if($Result_Month == $Value_Month){
       
            return $Result;
          } else {
            break;
          }
        }
      }
    }
    return false;
  }

#Testing Functions
$day     = '2,5,7';
$week    = '2,5';
$month   = '9,10';
$time    = '10:00:00';

echo getNextRuntime ($day,$week,$month,$time)

#Output

If you are running it on 13th September 2020 before 10:00 AM, then the output will be as below

```2020-09-13```

Upvotes: 0

Deleter
Deleter

Reputation: 790

If you have a date made by $day, $month and year, to get "next {weekday}" such as "next saturday" you can use this way:

echo date("Y-m-d",strtotime("next saturday",strtotime("$month/$day/$year")));

Be careful, in some cases dates like 02/09 and 09/02 are both plausible so you may have problems depending on what strtotime understands.

You can replace "next saturday" with some other nice stuff, examples:

strtotime("now");
strtotime("10 September 2000");
strtotime("+1 day");
strtotime("+1 week");
strtotime("+1 week 2 days 4 hours 2 seconds");
strtotime("next Thursday");
strtotime("last Monday");

More info here: https://www.php.net/manual/en/function.strtotime.php

Upvotes: 0

Related Questions