簡體   English   中英

PHP日期如何檢查星期幾

[英]PHP date how to check the day of the week

我需要創建一個調度程序任務,所以說我要創建一個任務

`6/24/2014` which is Tuesday the 4th week.

我有今天的約會

`7/27/2014` which is Tuesday the 4th week also

如何檢查PHP,以便如果今天檢查的是第4周星期二,請根據先前的任務創建一個新任務(我具有復制功能,所以主要的問題實際上只是日期部分)。

例如:

$date1 = new DateTime($fromdatabasetheoriginaltaskdate);
$date2 = new DateTime($todaysdate);

另外,假設原始任務在第5周,那么它將計為該月的最后一周,這意味着下個月該任務將在該月的最后一周創建(無論是第4周還是第5周) )。

如果我正確理解了您的問題,則希望在本月的第一,第二,第三周創建任務,該任務應該在下個月的第一,第二,第三周進行復制。 但是,如果它是在本月的最后一周創建的,則無論該月是4日還是5日,都應在下個月的上周創建。

如果是這樣,請嘗試:-

$date = "6/24/2014";
$strtotime = strtotime($date);


$array = array(1=>"first",2=>"second",3=>"third",4=>"fourth",5=>"fifth");

$current_month = date('n',$strtotime);
$day_num = date('d',$strtotime);
$day_word = date('l',$strtotime);
$next_month = date('F',strtotime('next month',$strtotime));
$next_week_month = date('n',strtotime('next '.$day_word, $strtotime));
$num_of_days = date('t',$strtotime);

$week = ceil($day_num/7);

$word = $array[$week];

if($next_week_month > $current_month){
    $word = 'last';
}

echo date('m/d/Y', strtotime($word.' '.$day_word.' of '.$next_month.' 2014')); //Outputs 2014-07-29

此外,該腳本還會檢查當前日期是否為該月的最后一天。 例如:-

2014年6月24日是最后一個星期二,但是第4周,6月有5周。

假設您在“此日期是該月的第N個星期二”中找出N時遇到麻煩。

如果執行DateTime::format("d"); 您取回數字,在這種情況下為24 然后,您可以除以7並四舍五入。

$date1 = new DateTime($a);
$date2 = new DateTime($b);
$days_in_a_week = 7;
$day1 = $date1->format("d");
$day2= $date2->format("d");
$which_week1 = ceil($day1/$days_in_a_week); //4
$which_week2 = ceil($day2/$days_in_a_week); //4
if ($date1->format("D") == $date2->format("D") && $which_week1 == $which_week2) {
    //do stuff
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM