Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want a loop like this:

This loop is OK:

while($d<=$d2) {  
        $x = "first day of + 1 month";
        $d=strtotime($x,$d);
        do_something($d);
}

But

while($d<=$d2) {  
        $x = "third day of + 1 month";
        $d=strtotime($x,$d);
        do_something($d);
}

doens't work: https://www.functions-online.com/strtotime.html gives "false" How can I solve this?

(first, second, third etc is a variable)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
325 views
Welcome To Ask or Share your Answers For Others

1 Answer

Without specifying a day of the week, only the first and last day work with "+ x months". It only remains to work twice with strtotime. Example:

$d1= strtotime('first day of + 1 month',$d);
$day = 3;
$dateStr = date("Y-m-",$d1).sprintf("%02d",$day);
$d = strtotime($dateStr);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...