php 检查日期是星期六

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5298757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:54:33  来源:igfitidea点击:

php check the date is Saturday

phpif-statement

提问by tonoslfx

how to check if the date is saturday.

如何检查日期是否是星期六。

<input id="datePicker" name="datePicker" type="text" class="textinput date-pick">

my code:

我的代码:

if(date('Y-m-d', strtotime('this Saturday')) == $_SESSION['search_date']) {
  echo 'Event this saturday'; 
} else {
  echo 'Event on the others day';
}

above code echoing only for the next week event! if i search for week after or 3 week etc, is not showing the result?

以上代码仅在下周活动中回显!如果我搜索一周后或 3 周等,没有显示结果?

回答by oezi

take a look at date()in the php-documentation. you chould change your code to something like this:

看看php 文档中的date()。你可以把你的代码改成这样:

if(date('w', strtotime($_SESSION['search_date'])) == 6) {
  echo 'Event is on a saturday'; 
} else {
  echo 'Event on the others day';
}

回答by Gareth

This should do it:

这应该这样做:

if(date("w",$timestamp)==6)
    echo "Saturday";

回答by ChrisH

Check: http://nl2.php.net/manual/en/function.date.phpdate('w', strtotime($_SESSION['search_date']))should give the weekday. Check if it's 6, wich is saturday.

检查:http: //nl2.php.net/manual/en/function.date.phpdate('w', strtotime($_SESSION['search_date']))应该给出工作日。检查它是否是 6,即星期六。

回答by Richard Rodriguez

date('l') returns a textual representation of the day in question, so I'd do this:

date('l') 返回相关日期的文本表示,所以我会这样做:

$date = strtotime($_SESSION['search_date']);
if (date('l', $date) == 'Saturday'){
 // you know the rest
}

回答by Tyson

//Just sharing

//these lines of codes returns "Holidays: Sat & Sun" based on given start and end date

date_default_timezone_set('Asia/Kuala Lumpure');

$startDate = '2014-01-03';

$endDate = '2014-01-23';

$st_arr = explode('-', $startDate);

$en_arr = explode('-', $endDate);

$st_tot = intval($st_arr[0]+$st_arr[1]+$st_arr[2]);

$en_tot = intval($en_arr[0]+$en_arr[1]+$en_arr[2]);

$count = 0;

for( $i = $st_tot ; $i <= $en_tot ; $i++ ) {

//Increase each day by count: goes according to the calender val

    $date = strtotime("+" .$count." day", strtotime($startDate));
    $x = date("Y-m-d", $date);

    if(date("w",strtotime($x))==6 || date("w",strtotime($x))==0 ) {
        echo "holiday - ". $x. '<br>';
    } else {
        echo "Nope - ". $x. '<br>';
    }

    $count++;
}