檢查日期是否是周末PHP這個函數(shù)似乎只返回false。你們中的任何人都一樣嗎?我確定我會忽略一些東西,然而,新鮮的眼睛和所有......function isweekend($date){
$date = strtotime($date);
$date = date("l", $date);
$date = strtolower($date);
echo $date;
if($date == "saturday" || $date == "sunday") {
return "true";
} else {
return "false";
}}我使用以下函數(shù)調(diào)用該函數(shù):$isthisaweekend = isweekend('2011-01-01');
3 回答

UYOU
TA貢獻(xiàn)1878條經(jīng)驗 獲得超4個贊
如果你有PHP> = 5.1:
function isWeekend($date) { return (date('N', strtotime($date)) >= 6);}
除此以外:
function isWeekend($date) { $weekDay = date('w', strtotime($date)); return ($weekDay == 0 || $weekDay == 6);}

吃雞游戲
TA貢獻(xiàn)1829條經(jīng)驗 獲得超7個贊
另一種方法是使用DateTime類,這樣你也可以指定時區(qū)。注意:PHP 5.3或更高版本。
// For the current datefunction isTodayWeekend() { $currentDate = new DateTime("now", new DateTimeZone("Europe/Amsterdam")); return $currentDate->format('N') >= 6;}
如果您需要能夠檢查某個日期字符串,則可以使用DateTime :: createFromFormat
function isWeekend($date) { $inputDate = DateTime::createFromFormat("d-m-Y", $date, new DateTimeZone("Europe/Amsterdam")); return $inputDate->format('N') >= 6;}
這種方式的優(yōu)點在于您可以在不改變PHP全局時區(qū)的情況下指定時區(qū),這可能會在其他腳本中引起副作用(例如Wordpress)。

守候你守候我
TA貢獻(xiàn)1802條經(jīng)驗 獲得超10個贊
這里:
function isweekend($year, $month, $day){ $time = mktime(0, 0, 0, $month, $day, $year); $weekday = date('w', $time); return ($weekday == 0 || $weekday == 6);}
- 3 回答
- 0 關(guān)注
- 523 瀏覽
添加回答
舉報
0/150
提交
取消