{"id":464,"date":"2025-06-10T20:49:04","date_gmt":"2025-06-10T12:49:04","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/php\/php-application-manual\/jcyy\/464.html"},"modified":"2025-06-10T20:49:04","modified_gmt":"2025-06-10T12:49:04","slug":"%e5%9c%a8php%e4%b8%ad%e5%a4%84%e7%90%86%e5%a4%8f%e4%bb%a4%e6%97%b6","status":"publish","type":"my1js","link":"https:\/\/www.zhaozhao123.cn\/php\/my1js\/464.html","title":{"rendered":"\u5728PHP\u4e2d\u5904\u7406\u590f\u4ee4\u65f6"},"content":{"rendered":"<p><body><\/p>\n<h2>&#20171;&#32461;<\/h2>\n<p>&#26377;&#25928;&#31649;&#29702;&#26085;&#20809;&#33410;&#32422;&#26102;&#38388;&#65288;Daylight Saving Time&#65292;&#31616;&#31216;DST&#65289;&#22312;&#24212;&#29992;&#31243;&#24207;&#20013;&#33267;&#20851;&#37325;&#35201;&#65292;&#20197;&#25552;&#20379;&#22522;&#20110;&#26102;&#38388;&#30340;&#21151;&#33021;&#12290;PHP&#20973;&#20511;&#20854;&#24378;&#22823;&#30340;&#26085;&#26399;&#21644;&#26102;&#38388;&#20989;&#25968;&#65292;&#20351;&#24320;&#21457;&#20154;&#21592;&#33021;&#22815;&#36731;&#26494;&#22788;&#29702;DST&#12290;&#26412;&#25945;&#31243;&#23558;&#24102;&#20320;&#20102;&#35299;&#22914;&#20309;&#22312;PHP&#20013;&#20351;&#29992;&#21508;&#31181;&#26041;&#27861;&#26469;&#22788;&#29702;DST&#12290;<\/p>\n<h2>&#29702;&#35299;&#26102;&#21306;&#21644;&#22799;&#20196;&#26102;<\/h2>\n<p>&#22312;&#24320;&#22987;&#32534;&#20889;&#20195;&#30721;&#20043;&#21069;&#65292;&#29702;&#35299;PHP&#22914;&#20309;&#34920;&#31034;&#26102;&#21306;&#21450;&#20854;&#22788;&#29702;&#22799;&#20196;&#26102;&#35843;&#25972;&#33267;&#20851;&#37325;&#35201;&#12290;PHP&#20381;&#36182;&#20110;TimeZone&#25968;&#25454;&#24211;&#65292;&#35813;&#25968;&#25454;&#24211;&#35760;&#24405;&#20102;&#19990;&#30028;&#19978;&#30340;&#26102;&#38388;&#21306;&#21450;&#20854;&#29305;&#24615;&#21644;&#22799;&#20196;&#26102;&#12290;<\/p>\n<p>&#31034;&#20363; 1&#65306;&#21019;&#24314; DateTimeZone &#23545;&#35937;<\/p>\n<pre><code>$timeZone = new DateTimeZone('America\/New_York');<\/code><\/pre>\n<p>&#36825;&#20010;&#29289;&#20307;&#23545;&#20110;&#21518;&#32493;&#30340;&#26102;&#38388;&#25805;&#20316;&#33267;&#20851;&#37325;&#35201;&#12290;<\/p>\n<h2>&#22522;&#26412;&#30340;&#26102;&#21306;&#25805;&#20316;<\/h2>\n<p>&#31034;&#20363; 2&#65306;&#20351;&#29992;&#26412;&#22320;&#22799;&#20196;&#26102;&#23454;&#20363;&#21270; DateTime<\/p>\n<pre><code>$dateTime = new DateTime('now', $timeZone);<\/code><\/pre>\n<p>&#26816;&#26597;&#25105;&#20204;&#26159;&#21542;&#22788;&#20110;&#22799;&#20196;&#26102;&#65292;&#38750;&#24120;&#31616;&#21333;&#65306;<\/p>\n<pre><code>echo $dateTime-&gt;format('I') ? 'DST' : 'No DST';<\/code><\/pre>\n<p>&#25105;&#20204;&#36824;&#21487;&#20197;&#24471;&#30693;&#22799;&#20196;&#26102;&#24320;&#22987;&#25110;&#32467;&#26463;&#30340;&#26085;&#26399;&#65306;<\/p>\n<pre><code>$dstStart = (new DateTime('last sunday of March ' . date('Y'), $timeZone))-&gt;setTime(2, 0);\n$dstEnd = (new DateTime('last sunday of October ' . date('Y'), $timeZone))-&gt;setTime(2, 0);<\/code><\/pre>\n<h2>&#32771;&#34385;&#22799;&#20196;&#26102;&#26102;&#21306;&#36716;&#25442;<\/h2>\n<p>&#35753;&#25105;&#20204;&#30475;&#30475;&#22914;&#20309;&#27491;&#30830;&#22320;&#22312;&#19981;&#21516;&#35266;&#23519;&#22799;&#20196;&#26102;&#26102;&#38388;&#21306;&#20043;&#38388;&#36716;&#25442;&#26102;&#38388;&#12290;<\/p>\n<p>&#31034;&#20363; 3&#65306;&#26102;&#21306;&#36716;&#25442;<\/p>\n<pre><code>$dateLondon = new DateTime('now', new DateTimeZone('Europe\/London'));\n$dateNewYork = clone $dateLondon;\n$dateNewYork-&gt;setTimezone(new DateTimeZone('America\/New_York'));<\/code><\/pre>\n<p>PHP &#20250;&#30456;&#24212;&#22320;&#35843;&#25972;&#26102;&#38388;&#65292;&#32771;&#34385;&#22799;&#20196;&#26102;&#65288;DST&#65289;&#12290;<\/p>\n<h2>&#39640;&#32423;DST&#22788;&#29702;&#19982;DateTimeInterface<\/h2>\n<p>&#25105;&#20204;&#21487;&#20197;&#20197;&#26356;&#25277;&#35937;&#30340;&#26041;&#24335;&#19982;DST&#20132;&#20114;&#65292;&#20351;&#29992;DateTimeInterface&#65292;&#35813;&#25509;&#21475;&#20301;&#20110;DateTime&#21644;DateTimeImmutable&#31867;&#20043;&#19979;&#12290;<\/p>\n<p>&#31034;&#20363; 4&#65306;&#28789;&#27963;&#30340;&#26102;&#21306;&#35843;&#25972;<\/p>\n<pre><code>$serverTime = new DateTimeImmutable();\n$clientTimezone = new DateTimeZone('America\/Los_Angeles');\n$clientTime = $serverTime-&gt;setTimezone($clientTimezone);\n\n<\/code><\/pre>\n<p>&#22240;&#27492;&#65292;&#25105;&#20204;&#36824;&#21487;&#20197;&#25552;&#20379;&#26356;&#31934;&#32454;&#30340;&#26102;&#38388;&#35843;&#25972;&#25511;&#21046;&#12290;<\/p>\n<h2>&#20020;&#26102;&#35206;&#30422;PHP&#30340;&#40664;&#35748;&#26102;&#21306;<\/h2>\n<p>&#20020;&#26102;&#26356;&#25913;&#40664;&#35748;&#26102;&#21306;&#22312;&#22788;&#29702;&#38656;&#35201;&#19981;&#21516;&#26102;&#38388;&#22522;&#30340;&#33050;&#26412;&#26102;&#38750;&#24120;&#26377;&#29992;&#12290;<\/p>\n<p>&#31034;&#20363; 5&#65306;&#35206;&#30422;&#26102;&#21306;<\/p>\n<pre><code>date_default_timezone_set('Europe\/Berlin');<\/code><\/pre>\n<p>&#35760;&#24471;&#23436;&#25104;&#20043;&#21518;&#23558;&#20854;&#37325;&#32622;&#22238;UTC&#25110;&#26381;&#21153;&#22120;&#40664;&#35748;&#30340;&#26102;&#38388;&#21306;&#65292;&#20197;&#20813;&#20135;&#29983;&#21103;&#20316;&#29992;&#12290;<\/p>\n<h2>&#26102;&#21306;&#36793;&#32536;&#24773;&#20917;&#22788;&#29702;<\/h2>\n<p>&#26102;&#21306;&#36716;&#25442;&#21487;&#33021;&#20250;&#29305;&#21035;&#26840;&#25163;&#65292;&#23588;&#20854;&#26159;&#22312;&#20999;&#25442;&#26399;&#38388;&#12290;&#20363;&#22914;&#65292;&#22312;&#20132;&#25509;&#29677;&#26102;&#38388;&#23433;&#25490;&#20107;&#20214;&#38656;&#35201;&#20180;&#32454;&#32771;&#34385;&#12290;<\/p>\n<p>&#31034;&#20363; 6&#65306;&#31649;&#29702;&#26102;&#21306;&#20999;&#25442;<\/p>\n<pre><code>$eventDate = new DateTime('2023-03-31 02:30:00', new DateTimeZone('America\/New_York'));\ntry {\n    $eventDate-&gt;setTime(2, 30);\n} catch (Exception $e) {\n    $eventDate-&gt;modify('+1 hour');\n}<\/code><\/pre>\n<p>&#36825;&#26159;&#19968;&#26465;&#22788;&#29702;&#22240;&#26102;&#21306;&#21464;&#21160;&#23548;&#33268;&#30340;&#19981;&#23384;&#22312;&#25110;&#37325;&#22797;&#26102;&#38388;&#30340;&#26041;&#27861;&#12290;<\/p>\n<h2>&#23433;&#25490;&#20219;&#21153;&#21644;&#22799;&#20196;&#26102;&#30340;&#21547;&#20041;<\/h2>\n<p>&#22312;&#24212;&#29992;&#31243;&#24207;&#20013;&#23433;&#25490;&#20219;&#21153;&#65292;&#29305;&#21035;&#26159;crontab&#20316;&#19994;&#26102;&#65292;&#35831;&#27880;&#24847;&#65292;&#40664;&#35748;&#24773;&#20917;&#19979;&#23427;&#20204;&#19981;&#20250;&#35782;&#21035;&#26085;&#20809;&#33410;&#32422;&#26102;&#38388;&#65288;DST&#65289;&#30340;&#26356;&#25913;&#12290;<\/p>\n<p>&#20363;&#23376; 7&#65306;&#26102;&#21306;&#25935;&#24863;&#30340;&#35843;&#24230;<\/p>\n<pre><code>$cronTime = new DateTimeZone('UTC')-&gt;getOffset(new DateTime('now', $timeZone)) !== $timeZone-&gt;getOffset(new DateTime('tomorrow', $timeZone));\n<\/code><\/pre>\n<p>&#24046;&#24322;&#34920;&#26126;&#38656;&#35201;&#23545;&#26102;&#21306;&#26356;&#25913;&#36827;&#34892;&#32534;&#31243;&#22788;&#29702;&#12290;<\/p>\n<h2>&#27979;&#35797;&#24744;&#30340;&#24212;&#29992;&#31243;&#24207;&#26102;&#20351;&#29992;DST<\/h2>\n<p>&#30830;&#20445;&#24744;&#30340;&#36719;&#20214;&#33021;&#22815;&#31283;&#20581;&#22320;&#22788;&#29702;&#26102;&#21306;&#21464;&#21270;&#65292;&#38656;&#35201;&#36827;&#34892;&#20840;&#38754;&#30340;&#27979;&#35797;&#26469;&#27169;&#25311;&#21508;&#31181;&#22330;&#26223;&#12290;<\/p>\n<p>&#20363;8&#65306;&#27169;&#25311;&#26085;&#38388;&#33410;&#32422;&#29992;&#30005;&#26465;&#20214;<\/p>\n<pre><code>function testDSTTransition(DateTimeZone $timeZone, $date) {\n    \/\/ Testing function code\n}<\/code><\/pre>\n<p>&#20026;&#20856;&#22411;&#21644;&#36793;&#32536;&#24773;&#20917;&#30340;&#26102;&#21306;&#36716;&#25442;&#21019;&#24314;&#27979;&#35797;&#29992;&#20363;&#65292;&#20197;&#39564;&#35777;&#27491;&#30830;&#34892;&#20026;&#12290;<\/p>\n<h2>&#32467;&#35770;&#12290;<\/h2>\n<p>&#22788;&#29702;PHP&#20013;&#30340;&#22799;&#20196;&#26102;&#38382;&#39064;&#38656;&#35201;&#20102;&#35299;DateTime&#21151;&#33021;&#20197;&#21450;&#26102;&#38388;&#21306;&#30340;&#29616;&#23454;&#24773;&#20917;&#12290;&#26412;&#25351;&#21335;&#21487;&#20197;&#20316;&#20026;&#36215;&#28857;&#12290;&#32784;&#24515;&#21644;&#24443;&#24213;&#30340;&#27979;&#35797;&#21487;&#20197;&#20351;&#24744;&#30340;PHP&#24212;&#29992;&#31243;&#24207;&#20687;&#38047;&#19968;&#26679;&#20934;&#30830;&#22320;&#22788;&#29702;&#22799;&#20196;&#26102;&#12290;<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#20171;&#32461; &#26377;&#25928;&#31649;&#29702;&#26085;&#20809;&#33410;&#32422;&#26102;&#38388;&#65288;Daylight Saving Time&#65292;&#31616;&#31216;DST&#65289;..<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":false},"tags":[],"my1js2nav":[38],"tuisongtax":[],"class_list":["post-464","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\/464","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=464"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tags?post=464"},{"taxonomy":"my1js2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js2nav?post=464"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tuisongtax?post=464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}