php检查日期是星期六

时间:2011-03-14 12:45:40

标签: php if-statement

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

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

我的代码:

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

以上代码仅针对下周活动回复!如果我搜索一周后或三周等,是不是显示结果?

5 个答案:

答案 0 :(得分:12)

在php文档中查看date()。您应该将代码更改为以下内容:

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

答案 1 :(得分:4)

这应该这样做:

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

答案 2 :(得分:1)

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

答案 3 :(得分:0)

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

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

答案 4 :(得分:0)

//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++;
}