0

I want to show an error if it's not a weekday (you can enter a date in the html section)

setlocale(LC_TIME, "de_DE.utf8");
$dateString = strftime('%A' , $dateTimestamp1);
substr($dateString, 0, 2);
if ($dateString == 'So' || $dateString == 'Sa') {
    throw new FormInputException('date', 'Invalid Date');
}
Aleks Beer
  • 158
  • 1
  • 8
Smuka
  • 13
  • 4

3 Answers3

1

You can call date('w', $date) where w will set the result to the numeric representation of the day of the week, where 0 represents Sunday, 1 is Monday ... 6 is Saturday etc. Therefore, with a simple custom function you can determine if a date falls on a weekday:

function dateIsWeekday($date) {
    $day = date('w', $date);
    return $day > 0 && $day < 6;
}

you can call:

if (!dateIsWeekday($dateTimestamp1)) {
    throw new FormInputException('date', 'Date is not a weekday');
}
Aleks Beer
  • 158
  • 1
  • 8
0

you did not state clearly what your problem is, because the approach you are using is basically ok. However there are a few things to keep in mind:

  1. A simpler way PHP already gives you the means to check for weekends (depending on your version of PHP). Check out this question that already has the answer: Weekend in PHP

  2. Using substr This is actually unnecessary. $dateString = strftime('%a' , $dateTimestamp1); will already give you the abbreviated string (small %a).

  3. Using LC_TIME An isuse might be that your time is not actually translated to German. This depends on if the language pack you are using (de-DE.utf8) is installed on your server or not. Actually using PHP's main methods for weekend checks you should not even need to care about things like this (see 1.).

Community
  • 1
  • 1
Gegenwind
  • 1,388
  • 1
  • 17
  • 27
0

if anybody else got the problem i fixed it- here is the code

$dateTimestamp1 = strtotime($this->date);

//setlocale(LC_TIME, "de_DE.utf8");
$day = date('N', $dateTimestamp1);
if ($day == 6 || $day == 7) {
    throw new FormInputException('date', 'Invalid Date');
}
Aleks Beer
  • 158
  • 1
  • 8
Smuka
  • 13
  • 4