[英]check if current week contains the last friday of the month
我正在嘗試創建一個腳本,如果當月的最后一個星期五是在當前周,則會更改頁面上的圖像。
例如,如果我在一周中的任何一天(星期一到星期日)中包含該星期的最后一個星期五,那么我將獲得與該月剩余時間不同的輸出。 但是我問了另一個基於此問題的問題,我問了類似的問題:
$last_friday = strtotime('last Friday of this month');
if (date("Y-m-d") == date("Y-m-d", $last_friday)) {
$output = "<img src='payday.jpg' />";
} else {
$output = "<img src='nomoneyyet.jpg' />;
}
但經過測試后,它在實際的一周內無效。 strtotime我應該使用什么?
使用PHP中的DateTime類和格式輸出進行比較:
// Be sure to check your timezone `date_default_timezone_set`
$today = new DateTime();
$last_friday = new DateTime('last Friday of this month');
// For testing
$friday_april = new DateTime('2014-4-25');
if ($today->format('Y-m-d') === $last_friday->format('Y-m-d')) {
print 'Today is friday';
}
if ($friday_april->format('Y-m-d') === $last_friday->format('Y-m-d')) {
print 'Yes, a test friday is also a friday';
}
使用DateTime作為
$date = new DateTime();
$date->modify('last friday of this month');
$current_date = new DateTime() ;
if($date == $current_date){
$output = "<img src='payday.jpg' />";
}else{
$output = "<img src='nomoneyyet.jpg' />";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.