{"id":455,"date":"2025-06-10T20:41:32","date_gmt":"2025-06-10T12:41:32","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/php\/php-application-manual\/jcyy\/455.html"},"modified":"2025-06-10T20:41:32","modified_gmt":"2025-06-10T12:41:32","slug":"php%ef%bc%9a%e5%b0%86utc%e6%97%b6%e9%97%b4%e8%bd%ac%e6%8d%a2%e4%b8%ba%e6%9c%ac%e5%9c%b0%e6%97%b6%e9%97%b4%e5%8f%8a%e7%9b%b8%e5%8f%8d%e8%bd%ac%e6%8d%a2","status":"publish","type":"my1js","link":"https:\/\/www.zhaozhao123.cn\/php\/my1js\/455.html","title":{"rendered":"PHP\uff1a\u5c06UTC\u65f6\u95f4\u8f6c\u6362\u4e3a\u672c\u5730\u65f6\u95f4\u53ca\u76f8\u53cd\u8f6c\u6362"},"content":{"rendered":"<p><body><\/p>\n<h2>&#27010;&#35272;<\/h2>\n<p>&#19982;&#26102;&#21306;&#24037;&#20316;&#21487;&#20197;&#26159;&#24320;&#21457;Web&#24212;&#29992;&#31243;&#24207;&#30340;&#20851;&#38190;&#26041;&#38754;&#12290;&#26412;&#25945;&#31243;&#23558;&#25351;&#23548;&#24744;&#36890;&#36807;PHP&#36827;&#34892;UTC&#26102;&#38388;&#21040;&#26412;&#22320;&#26102;&#38388;&#21644;&#21453;&#20043;&#30340;&#36716;&#25442;&#65292;&#30830;&#20445;&#24744;&#21487;&#20197;&#33258;&#20449;&#22320;&#22788;&#29702;&#26102;&#38388;&#36716;&#25442;&#12290;<\/p>\n<h2>&#29702;&#35299;PHP&#20013;&#30340;&#26102;&#21306;&#38382;&#39064;<\/h2>\n<p>&#22312;PHP&#20013;&#19982;&#26102;&#21306;&#30456;&#20851;&#30340;&#24037;&#20316;&#65292;&#38656;&#35201;&#20351;&#29992;timezone&#20989;&#25968;&#21644;&#35774;&#32622;&#26102;&#21306;&#30340;&#21151;&#33021;&#12290;<code>DateTime<\/code>&#31867;&#21450;&#20854;&#30456;&#20851;&#21151;&#33021;&#36890;&#24120;&#34987;&#20351;&#29992;&#12290;&#23427;&#20204;&#26159;&#38754;&#21521;&#23545;&#35937;&#30340;&#65292;&#25552;&#20379;&#20102;&#20016;&#23500;&#30340;&#36716;&#25442;&#21644;&#26684;&#24335;&#21270;&#36873;&#39033;&#12290;<\/p>\n<pre><code>$now = new DateTime();\n$now-&gt;setTimezone(new DateTimeZone('UTC'));\necho $now-&gt;format('Y-m-d H:i:s');<\/code><\/pre>\n<p>&#35813;&#29255;&#27573;&#21019;&#24314;&#20102;&#19968;&#20010;&#26032;&#30340;<code>DateTime<\/code>&#24403;&#21069;&#26102;&#38388;&#30340;&#23545;&#35937;&#35774;&#32622;&#20854;&#26102;&#21306;&#20026;UTC&#65292;&#28982;&#21518;&#20197;&#26631;&#20934;&#26684;&#24335;&#36755;&#20986;&#12290;<\/p>\n<h2>&#23558;UTC&#36716;&#25442;&#20026;&#26412;&#22320;&#26102;&#38388;<\/h2>\n<p>&#20026;&#20102;&#23558;UTC&#36716;&#25442;&#20026;&#26412;&#22320;&#26102;&#38388;&#65292;&#20320;&#38656;&#35201;&#30693;&#36947;&#20320;&#24819;&#23558;&#20854;&#36716;&#25442;&#20026;&#30446;&#26631;&#30340;&#26412;&#22320;&#26102;&#21306;&#12290;&#20351;&#29992;PHP&#30340;<code>DateTimeZone<\/code>&#22788;&#29702;&#36825;&#20010;&#38382;&#39064;&#12290;<\/p>\n<pre><code>$utcTime     = new DateTime('now', new DateTimeZone('UTC'));\n$localTZ     = new DateTimeZone('America\/New_York');\n$localTime   = clone $utcTime;\n$localTime-&gt;setTimezone($localTZ);\necho $localTime-&gt;format('Y-m-d H:i:s');<\/code><\/pre>\n<p>&#22312;&#31034;&#20363;&#20013;&#65292;UTC&#26102;&#38388;&#34987;&#36716;&#25442;&#20026;&#32445;&#32422;&#26102;&#38388;&#12290;&#20320;&#21487;&#20197;&#29992;&#20219;&#20309;&#25903;&#25345;&#30340;&#26102;&#38388;&#21306;&#26367;&#25442;&ldquo;America\/New_York&rdquo;&#12290;<\/p>\n<h2>&#23558;&#26412;&#22320;&#26102;&#38388;&#36716;&#25442;&#20026;UTC&#12290;<\/h2>\n<p>&#20174;&#26412;&#22320;&#26102;&#38388;&#36716;&#25442;&#20026;UTC&#31867;&#20284;&#20110;&#65292;&#36890;&#36807;&#23558;&#29616;&#26377;datetime&#23545;&#35937;&#30340;&#26102;&#21306;&#35774;&#32622;&#20026;UTC&#26469;&#23436;&#25104;&#65306;<\/p>\n<pre><code>$localTime   = new DateTime('now', new DateTimeZone('America\/New_York'));\n$utcTime = clone $localTime;\n$utcTime-&gt;setTimezone(new DateTimeZone('UTC'));\necho $utcTime-&gt;format('Y-m-d H:i:s');<\/code><\/pre>\n<p>&#36825;&#20250;&#23558;&#19968;&#20010;&#32445;&#32422;&#26412;&#22320;&#26102;&#38388;&#36716;&#25442;&#22238;UTC&#26102;&#38388;&#12290;<\/p>\n<h2>&#22788;&#29702;&#26085;&#20809;&#33410;&#32422;&#26102;&#24046;<\/h2>\n<p>&#22799;&#20196;&#26102;&#20250;&#23548;&#33268;&#26102;&#38388;&#36716;&#25442;&#21464;&#24471;&#22797;&#26434;&#65292;&#20294;&#24184;&#36816;&#30340;&#26159;&#65292;&#22312;&#25351;&#23450;&#36866;&#24403;&#26102;&#21306;&#30340;&#24773;&#20917;&#19979;&#65292;PHP &#20250;&#33258;&#21160;&#35843;&#25972;&#20197;&#22788;&#29702; DST&#65288;&#26085;&#20809;&#33410;&#32422;&#26102;&#38388;&#65289;&#12290;<\/p>\n<pre><code>$dt = new DateTime('now', new DateTimeZone('America\/New_York'));\necho $dt-&gt;format('I') ? 'DST is in effect' : 'DST is not in effect';<\/code><\/pre>\n<p>&#26816;&#26597;&ldquo;I&rdquo;&#26684;&#24335;&#21487;&#20197;&#21578;&#35785;&#20320;&#24403;&#21069;&#26159;&#21542;&#22788;&#20110;&#22799;&#20196;&#26102;&#12290;<\/p>\n<h2>&#39640;&#32423;&#26102;&#38388;&#36716;&#25442;&#25216;&#26415;<\/h2>\n<p>&#23545;&#20110;&#26356;&#22797;&#26434;&#30340;&#22330;&#26223;&#65292;PHP &#20063;&#25552;&#20379;&#20102;&#30456;&#24212;&#30340;&#21151;&#33021;&#12290;<code>DateTimeImmutable<\/code>&#23545;&#19981;&#36215;&#65292;&#25105;&#26080;&#27861;&#29702;&#35299;&#36825;&#21477;&#35805;&#30340;&#19978;&#19979;&#25991;&#12290;&#35831;&#25552;&#20379;&#26356;&#22810;&#30340;&#20449;&#24687;&#25110;&#37325;&#26032;&#34920;&#36848;&#24744;&#30340;&#38382;&#39064;&#12290;<code>DateTime::add()<\/code>and &#26159;&#20013;&#25991;&#37324;&#30340;&ldquo;&#21644;&rdquo;&#65292;&#34920;&#31034;&#24182;&#21015;&#20851;&#31995;&#12290;<code>DateTime::sub()<\/code>&#26041;&#27861;&#20043;&#19968;&#26159;&#22312;&#19981;&#25913;&#21464;&#21407;&#22987;&#23545;&#35937;&#30340;&#24773;&#20917;&#19979;&#25805;&#32437;&#26085;&#26399;&#21644;&#26102;&#38388;&#12290;<\/p>\n<pre><code>$dtUTC    = new DateTimeImmutable('now', new DateTimeZone('UTC'));\n$dtNY     = $dtUTC-&gt;setTimezone(new DateTimeZone('America\/New_York'));\n$interval = new DateInterval('P1D');\n$dtPlusOneDay = $dtNY-&gt;add($interval);\necho $dtPlusOneDay-&gt;format('Y-m-d H:i:s');<\/code><\/pre>\n<p>&#36825;&#37324;&#65292;&#19968;&#22825;&#30340;&#26102;&#38388;&#24046;&#34987;&#21152;&#21040;&#32445;&#32422;&#26102;&#38388;&#37324;&#65292;&#26174;&#31034;&#25509;&#19979;&#26469;&#19968;&#22825;&#30340;&#26085;&#26399;&#21644;&#26102;&#38388;&#12290;<\/p>\n<h2>&#19982;&#26102;&#38388;&#25139;&#24037;&#20316;<\/h2>\n<p>&#33258;UNIX&#26102;&#38388;&#25139;&#22987;&#32456;&#22312;UTC&#65292;&#21482;&#38656;&#22312;&#21021;&#22987;&#21270;DateTime&#23545;&#35937;&#26102;&#35774;&#32622;&#26102;&#21306;&#65292;&#28982;&#21518;&#23601;&#21487;&#20197;&#23558;&#23427;&#20204;&#36716;&#25442;&#20026;&#26412;&#22320;&#26102;&#38388;&#20102;&#12290;<\/p>\n<pre><code>$timestamp = time();\n$dt = new DateTime('@'.$timestamp);\n$dt-&gt;setTimezone(new DateTimeZone('America\/New_York'));\necho $dt-&gt;format('Y-m-d H:i:s');<\/code><\/pre>\n<p>&#26143;&#21495;&#65288;@&#65289;&#21069;&#32512;&#34920;&#31034;&#25509;&#19979;&#26469;&#30340;&#25968;&#23383;&#20026;UNIX&#26102;&#38388;&#25139;&#12290;<\/p>\n<h2>&#26102;&#38388;&#36716;&#25442;&#22312;&#29992;&#25143;&#24212;&#29992;&#20013;&#30340;&#22788;&#29702;&#26041;&#27861;<\/h2>\n<p>&#23545;&#20110;&#38754;&#21521;&#29992;&#25143;&#30340;&#24212;&#29992;&#65292;&#36890;&#24120;&#38656;&#35201;&#23558;&#26102;&#38388;&#36716;&#25442;&#20026;&#29992;&#25143;&#25152;&#22312;&#22320;&#30340;&#26102;&#38388;&#21306;&#12290;&#20320;&#21487;&#20197;&#20174;&#20182;&#20204;&#30340;&#27983;&#35272;&#22120;&#33719;&#21462;&#29992;&#25143;&#30340;&#26102;&#38388;&#21306;&#65292;&#25110;&#32773;&#22312;&#20320;&#30340;&#24212;&#29992;&#20869;&#35753;&#20182;&#20204;&#36873;&#25321;&#12290;<\/p>\n<pre><code>\/\/ PHP would receive a timezone string, e.g., $_POST['user_timezone']\n$userTimezone = new DateTimeZone('Europe\/Berlin');\n$utcTime = new DateTime('now', new DateTimeZone('UTC'));\n$userTime = clone $utcTime;\n$userTime-&gt;setTimeZone($userTimezone);\necho $userTime-&gt;format('Y-m-d H:i:s');\n<\/code><\/pre>\n<p>&#22312;&#22788;&#29702;&#29992;&#25143;&#36755;&#20837;&#30340;&#26102;&#21306;&#36716;&#25442;&#26102;&#35201;&#23567;&#24515;&#65292;&#22987;&#32456;&#39564;&#35777;&#21644;&#28165;&#29702;&#36755;&#20837;&#30340;&#26102;&#21306;&#23383;&#31526;&#20018;&#12290;<\/p>\n<h2>&#32467;&#35770;&#12290;<\/h2>\n<p>&#22312;PHP&#20013;&#36716;&#25442;UTC&#21644;&#26412;&#22320;&#26102;&#38388;&#30456;&#23545;&#31616;&#21333;&#65292;&#38656;&#35201;&#27491;&#30830;&#20351;&#29992;&#30456;&#20851;&#30340;&#20989;&#25968;&#12290;<code>DateTime<\/code>&#31867;&#21450;&#20854;&#26041;&#27861;&#12290;&#36890;&#36807;&#36981;&#24490;&#19978;&#36848;&#31034;&#20363;&#65292;&#20320;&#21487;&#20197;&#23454;&#29616;&#26102;&#38388;&#36716;&#25442;&#21151;&#33021;&#65292;&#35813;&#21151;&#33021;&#20250;&#33258;&#21160;&#32771;&#34385;&#35832;&#22914;&#22799;&#20196;&#26102;&#35843;&#25972;&#31561;&#26085;&#26399;\/&#26102;&#38388;&#30456;&#20851;&#22240;&#32032;&#12290;&#35831;&#21153;&#24517;&#20351;&#29992;PHP&#30340;&#26102;&#21306;&#22788;&#29702;&#26469;&#30830;&#20445;&#22312;&#24212;&#29992;&#31243;&#24207;&#20013;&#30340;&#26102;&#38388;&#25935;&#24863;&#25805;&#20316;&#30340;&#20934;&#30830;&#24615;&#21644;&#21487;&#38752;&#24615;&#12290;<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#27010;&#35272; &#19982;&#26102;&#21306;&#24037;&#20316;&#21487;&#20197;&#26159;&#24320;&#21457;Web&#24212;&#29992;&#31243;&#24207;&#30340;&#20851;&#38190;&#038;#26..<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":false},"tags":[],"my1js2nav":[38],"tuisongtax":[],"class_list":["post-455","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\/455","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=455"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tags?post=455"},{"taxonomy":"my1js2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js2nav?post=455"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tuisongtax?post=455"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}