简体   繁体   中英

Filter Working days from weekend php

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');
}

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');
}

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.).

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');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM