I have the following code:
$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"));
}
The original two dates it gives me are correct but when I try to jump back a week I get an error saying A non well formed numeric value encountered
.
I am not sure why this is happening or how I should fix this, any suggestions would be great.
CodePudding user response:
strtotime(time, now); Required. Specifies a date/time string
use this way to get currect output
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)));
}
?>
CodePudding user response:
The function strotime
expects a date string in the first argument and in the second argument a timestamp as base time.
You can change your code to use the DateTime
object and manipulate the date on this.
<?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");
}