我有以下代碼:
$last_week_start = date("Y-m-d",strtotime("monday last week"));
$last_week_end = date("Y-m-d",strtotime("sunday last week"));
for($i=52;$i>0;$i--){
//do stuff with the dates for this week
$last_week_start = date("Y-m-d",strtotime($last_week_start,"-7 days"));
$last_week_end = date("Y-m-d",strtotime($last_week_end,"-7 days"));
}
它給我的最初的兩個日期是正確的,但是當我嘗試跳回一周時,我收到一個錯誤訊息A non well formed numeric value encountered。
我不確定為什么會發生這種情況或我應該如何解決這個問題,任何建議都會很棒。
uj5u.com熱心網友回復:
strtotime(time, now); Required. Specifies a date/time string
使用這種方式獲得正確的輸出
date("Y-m-d",strtotime("-7 days",strtotime($last_week_start)))
<?PHP
$last_week_start = date("Y-m-d",strtotime("monday last week"));
$last_week_end = date("Y-m-d",strtotime("sunday last week"));
echo $last_week_start." ".$last_week_end;
for($i=52;$i>0;$i--){
//do stuff with the dates for this week
$last_week_start = date("Y-m-d",strtotime("-7 days",strtotime($last_week_start)));
$last_week_end = date("Y-m-d",strtotime("-7 days", strtotime($last_week_end)));
}
?>
uj5u.com熱心網友回復:
該函式strotime在第一個引數中需要一個日期字串,在第二個引數中需要一個時間戳作為基準時間。
您可以更改代碼以使用該DateTime物件并在此操作日期。
<?php
$last_week_start = new \DateTime("monday last week");
$last_week_end = new \DateTime("sunday last week");
for($i=52;$i>0;$i--){
//do stuff with the dates for this week
$last_week_start->modify("-7 days");
$last_week_end->modify("-7 days");
$last_week_start_formatted = $last_week_start->format("Y-m-d");
$last_week_end_formatted = $last_week_end->format("Y-m-d");
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/465500.html
上一篇:我可以使用JRE17運行使用Java8(.192)構建的Java程式,還是所有東西(JRE、JVM、JDK)都必須首先使用正確的Java8版本?
