{"id":458,"date":"2025-06-10T20:43:47","date_gmt":"2025-06-10T12:43:47","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/php\/php-application-manual\/jcyy\/458.html"},"modified":"2025-06-10T20:43:47","modified_gmt":"2025-06-10T12:43:47","slug":"php%ef%bc%9a%e8%8e%b7%e5%8f%96%e4%b8%a4%e4%b8%aa%e6%97%a5%e6%9c%9f%e4%b9%8b%e9%97%b4%e7%9a%84%e6%97%a5%e6%9c%9f%e6%95%b0%e7%bb%84","status":"publish","type":"my1js","link":"https:\/\/www.zhaozhao123.cn\/php\/my1js\/458.html","title":{"rendered":"PHP\uff1a\u83b7\u53d6\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u65e5\u671f\u6570\u7ec4"},"content":{"rendered":"<p><body><\/p>\n<h2>&#20171;&#32461;<\/h2>\n<p>&#22312;Web&#24320;&#21457;&#20013;&#30340;PHP&#20013;&#65292;&#26377;&#26102;&#38656;&#35201;&#35745;&#31639;&#20004;&#20010;&#25351;&#23450;&#26085;&#26399;&#20043;&#38388;&#30340;&#33539;&#22260;&#12290;&#36825;&#21487;&#20197;&#29992;&#20110;&#29983;&#25104;&#25253;&#21578;&#12289;&#21019;&#24314;&#39044;&#35746;&#26085;&#26399;&#33539;&#22260;&#25110;&#23384;&#20648;&#19968;&#20010;&#36830;&#32493;&#30340;&#26085;&#26399;&#25968;&#32452;&#12290;&#22312;&#36825;&#31687;&#25945;&#31243;&#20013;&#65292;&#25105;&#20204;&#23558;&#25506;&#35752;&#20960;&#31181;&#23454;&#29616;&#27492;&#30446;&#30340;&#30340;&#26041;&#27861;&#12290;<\/p>\n<h2>&#20351;&#29992;DateTime&#21644;DateInterval&#12290;<\/h2>\n<p>DateTime&#21644;DateInterval&#31867;&#25552;&#20379;&#20102;&#19968;&#31181;&#20197;&#38754;&#21521;&#23545;&#35937;&#30340;&#26041;&#24335;&#22788;&#29702;&#26085;&#26399;&#30340;&#24378;&#22823;&#32452;&#21512;&#12290;&#36825;&#37324;&#26377;&#19968;&#20010;&#22522;&#26412;&#30340;&#20363;&#23376;&#65292;&#22914;&#20309;&#33719;&#21462;&#20004;&#20010;&#26085;&#26399;&#20043;&#38388;&#30340;&#26085;&#26399;&#25968;&#32452;&#65306;<\/p>\n<pre><code>\n$start = new DateTime('2021-01-01');\n$end = new DateTime('2021-01-10');\n\/\/Create a DateInterval object with a 1 day interval\n$interval = new DateInterval('P1D');\n\n$dates = [];\nfor($i = $start; $i &lt;= $end; $i-&gt;add($interval)){\n  $dates[] = $i-&gt;format('Y-m-d');\n}\nprint_r($dates);\n<\/code><\/pre>\n<p>&#36825;&#20010;&#22522;&#26412;&#31034;&#20363;&#21482;&#26159;&#21019;&#24314;&#20102;&#20004;&#20010;&#26085;&#26399;&#26102;&#38388;&#23545;&#35937;&#65292;&#20998;&#21035;&#20026;&#36215;&#22987;&#21644;&#32467;&#26463;&#26085;&#26399;&#65292;&#24182;&#22312;&#24490;&#29615;&#20013;&#28155;&#21152;&#19968;&#20010;&#26032;&#30340;&#26085;&#26399;&#38388;&#38548;&#65288;&#22312;&#36825;&#31181;&#24773;&#20917;&#19979;&#20026;&#19968;&#22825;&#65289;&#65292;&#30452;&#21040;&#36798;&#21040;&#32467;&#26463;&#26085;&#26399;&#12290;&#28982;&#21518;&#65292;&#27599;&#20010;&#26085;&#26399;&#37117;&#20197;&#23383;&#31526;&#20018;&#26684;&#24335;&#26684;&#24335;&#21270;&#24182;&#28155;&#21152;&#21040;&#25968;&#32452;&#20013;&#65292;&#21487;&#20197;&#26681;&#25454;&#38656;&#35201;&#20351;&#29992;&#12290;<\/p>\n<h2>&#20351;&#29992;DatePeriod&#31867;<\/h2>\n<p>&#26356;&#39640;&#32423;&#19988;&#31616;&#27905;&#30340;&#26041;&#27861;&#26159;&#22312;PHP&#20013;&#20351;&#29992;DatePeriod&#31867;&#26469;&#36798;&#21040;&#30456;&#21516;&#30340;&#25928;&#26524;&#65306;<\/p>\n<pre><code>\n$start = new DateTime('2021-01-01');\n$end = (new DateTime('2021-01-10'))-&gt;modify('+1 day');\n$interval = new DateInterval('P1D');\n\n$period = nre DatePeriod($start, $interval, $end);\nforeach ($period as $date) {\n  $dates[] = $date-&gt;format('Y-m-d');\n}\nprint_r($dates);\n<\/code><\/pre>\n<p>&#26085;&#26399;&#21608;&#26399;&#21019;&#24314;&#20102;&#19968;&#20010;&#26102;&#38388;&#33539;&#22260;&#65292;&#20854;&#20013;&#20320;&#21487;&#20197;&#36845;&#20195;&#27599;&#20010;&#38388;&#38548;&#12290;&#20363;&#22914;&#65292;&#19968;&#20010;&#20855;&#26377;&#19968;&#22825;&#38388;&#38548;&#30340;&#26085;&#26399;&#21608;&#26399;&#65292;&#36845;&#20195;&#23427;&#20250;&#29983;&#25104;&#20174;&#24320;&#22987;&#26085;&#26399;&#21040;&#32467;&#26463;&#26085;&#26399;&#27599;&#22825;&#30340;&#19968;&#20010;DateTime&#23545;&#35937;&#12290;&#35831;&#27880;&#24847;&#65292;$end&#26085;&#26399;&#24050;&#34987;&#20462;&#25913;&#20197;&#21253;&#21547;&#33539;&#22260;&#20869;&#30340;&#26368;&#21518;&#19968;&#20010;&#26085;&#26399;&#12290;<\/p>\n<h2>&#22788;&#29702;&#19981;&#21516;&#26102;&#38388;&#38388;&#38548;&#30340;&#24773;&#20917;&#12290;<\/h2>\n<p>&#25105;&#20204;&#24050;&#32463;&#32771;&#34385;&#20102;&#27599;&#26085;&#26102;&#38388;&#38388;&#38548;&#65292;&#20294;PHP&#30340;DateInterval&#31867;&#21487;&#20197;&#22788;&#29702;&#22810;&#31181;&#26102;&#38388;&#38388;&#38548;&#65292;&#20363;&#22914;&#27599;&#21608;&#12289;&#27599;&#26376;&#29978;&#33267;&#27599;&#24180;&#30340;&#26102;&#38388;&#38388;&#38548;&#12290;&#20197;&#19979;&#26159;&#20462;&#25913;&#21069;&#20363;&#20197;&#25509;&#21463;&#19981;&#21516;&#26102;&#38388;&#38388;&#38548;&#30340;&#26041;&#27861;&#65306;<\/p>\n<pre><code>\n$intervalSpec = 'P1W'; \/\/ 1 week\n$interval = new DateInterval($intervalSpec);\n\n\/\/ Use either the for loop or the DatePeriod as previously demonstrated\n<\/code><\/pre>\n<h2>&#22788;&#29702;&#22823;&#26085;&#26399;&#33539;&#22260;&#30340;&#24773;&#20917;&#12290;<\/h2>\n<p>&#22788;&#29702;&#38750;&#24120;&#22823;&#30340;&#26085;&#26399;&#33539;&#22260;&#26102;&#65292;&#20445;&#25345;&#20869;&#23384;&#20351;&#29992;&#25928;&#29575;&#21464;&#24471;&#37325;&#35201;&#12290;&#22914;&#26524;&#20351;&#29992;PHP 5.5&#25110;&#26356;&#39640;&#29256;&#26412;&#65292;&#21487;&#20197;&#32771;&#34385;&#20351;&#29992;&#29983;&#25104;&#22120;&#20989;&#25968;&#26469;&#36880;&#20010;&#29983;&#25104;&#26085;&#26399;&#65306;<\/p>\n<pre><code>\nfunction createDateRangeGenerator($start, $end, $intervalSpec = 'P1D') {\n  $start = new DateTime($start);\n  $end = new DateTime($end);\n  $interval = new DateInterval($intervalSpec);\n\n  $period = new DatePeriod($start, $interval, $end);\n  foreach ($period as $date) {\n    yield $date-&gt;format('Y-m-d');\n  }\n}\n\n\/\/ Usage\nforeach (createDateRangeGenerator('2021-01-01', '2021-01-10') as $date) {\n  echo $date . \"n\";\n}\n<\/code><\/pre>\n<p>&#36825;&#20010;&#29983;&#25104;&#22120;&#22312;&#19981;&#23558;&#25972;&#20010;&#25968;&#32452;&#21152;&#36733;&#21040;&#20869;&#23384;&#30340;&#24773;&#20917;&#19979;&#65292;&#21160;&#24577;&#22320;&#21019;&#24314;&#26085;&#26399;&#33539;&#22260;&#12290;<\/p>\n<h2>&#36793;&#32536;&#24773;&#20917;&#22788;&#29702;<\/h2>\n<p>&#22312;&#26500;&#24314;&#29992;&#20110;&#33719;&#21462;&#26085;&#26399;&#33539;&#22260;&#30340;&#23454;&#29992;&#20989;&#25968;&#26102;&#65292;&#38656;&#35201;&#32771;&#34385;&#36793;&#32536;&#24773;&#20917;&#65292;&#20363;&#22914;&#65306;<\/p>\n<p>&#26159;&#21542;&#24212;&#35813;&#21253;&#25324;&#32467;&#26463;&#26085;&#26399;&#36824;&#26159;&#25490;&#38500;&#22312;&#33539;&#22260;&#20043;&#22806;&#65311;<\/p>\n<p>&#22914;&#20309;&#22788;&#29702;&#26102;&#21306;&#65311;<\/p>\n<p>&#22914;&#26524;&#24320;&#22987;&#26085;&#26399;&#26202;&#20110;&#32467;&#26463;&#26085;&#26399;&#65292;&#35813;&#24590;&#20040;&#21150;&#65311;<\/p>\n<p>&#32771;&#34385;&#36825;&#20123;&#22240;&#32032;&#20250;&#20351;&#20320;&#30340;&#20195;&#30721;&#26356;&#21152;&#20581;&#22766;&#21644;&#21487;&#39044;&#27979;&#12290;&#22312;&#36935;&#21040;&#36793;&#32536;&#24773;&#20917;&#26102;&#65292;&#25243;&#20986;&#24322;&#24120;&#25110;&#36820;&#22238;&#26377;&#24847;&#20041;&#30340;&#38169;&#35823;&#28040;&#24687;&#26159;&#19968;&#31181;&#22909;&#30340;&#23454;&#36341;&#12290;<\/p>\n<h2>&#32467;&#35770;<\/h2>\n<p>&#29983;&#25104;PHP&#20013;&#20004;&#20010;&#29305;&#23450;&#26085;&#26399;&#20043;&#38388;&#30340;&#26085;&#26399;&#25968;&#32452;&#26159;&#19968;&#39033;&#24120;&#35265;&#30340;&#20219;&#21153;&#65292;&#21487;&#20197;&#20248;&#38597;&#22320;&#36890;&#36807;DateTime&#12289;DateInterval&#21644;DatePeriod&#31867;&#26469;&#35299;&#20915;&#12290;&#36825;&#20123;&#25552;&#20379;&#20102;&#26085;&#26399;&#21644;&#26102;&#38388;&#25805;&#20316;&#30340;&#38754;&#21521;&#23545;&#35937;&#26041;&#27861;&#12290;&#22312;&#23454;&#29616;&#27492;&#31867;&#21151;&#33021;&#26102;&#65292;&#35831;&#22987;&#32456;&#32771;&#34385;&#36793;&#32536;&#24773;&#20917;&#65292;&#24182;&#30830;&#20445;&#24744;&#30340;&#26085;&#26399;&#30456;&#20851;&#21151;&#33021;&#26082;&#24378;&#22823;&#21448;&#21487;&#38752;&#12290;<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#20171;&#32461; &#22312;Web&#24320;&#21457;&#20013;&#30340;PHP&#20013;&#65292;&#26377;&#26102;&#38656;&#35201;&#35745;&#31639;&#20004;&#20010;&#25351;&#23450;&#038;..<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":false},"tags":[],"my1js2nav":[38],"tuisongtax":[],"class_list":["post-458","my1js","type-my1js","status-publish","hentry","my1js2nav-jcyy"],"acf":{"qian_art_seotitle":"","qian_art_seotitle_source":{"label":"SEO\u6807\u9898","type":"text","formatted_value":""},"qian_art_seokws":"","qian_art_seokws_source":{"label":"SEO\u5173\u952e\u8bcd","type":"text","formatted_value":""},"qian_art_stzhong":"","qian_art_stzhong_source":{"label":"\u4e2d | \u77ed\u6807\u9898","type":"text","formatted_value":""}},"_links":{"self":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js\/458","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js"}],"about":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/types\/my1js"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/users\/1"}],"wp:attachment":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/media?parent=458"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tags?post=458"},{"taxonomy":"my1js2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js2nav?post=458"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tuisongtax?post=458"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}