簡體   English   中英

如何將PHP strtotime與“本周一”與時間結合使用?

[英]How to exactly use PHP strtotime with 'this monday' combined with a time?

我在PHP函數strtotime中遇到問題,在該函數中,我們嘗試獲取某個特定的第二天+時間的日期。

我想要的是獲取每個下一個'monday 20:00H'的下一個日期和時間。 (我們使用24小時制)

$next_moment = strtotime('this monday 20:00');

上面的代碼完全符合我們想要的內容,但是例如在10日星期一,則在20:00H之后,它仍然表示下一次發生是10日星期一20:00。

我必須等到星期一24:00H之后,與星期二00:00H相同。 在那之后,結果再次正確,並顯示星期一17th 20:00H。

請幫助糾正此問題。

嘗試這個:

$next_valid_date_time = strtotime('this Monday 20:00');

if(time() > $next_valid_date_time) {
    $next_valid_date_time = strtotime('Monday next week 20:00');
} 

echo date('Y-m-d H:i', $next_valid_date_time);

嘗試這個:

$next_moment = strtotime('this Monday 20:00');

if(time() > $next_moment) {
    $next_moment = strtotime('Monday next week 20:00');
} 

echo date('c',$next_moment);

檢查當前時間戳是否大於目標時間戳。

$next_moment = ( time() > strtotime( "Monday 20:00" ) ? strtotime( "next Monday 20:00" ) : strtotime( "Monday 20:00" ) );
echo ( date( 'Y-m-d H:i:s', $next_moment ) );

更改測試的日期和時間。

PHP相對格式

暫無
暫無

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

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