{"id":465,"date":"2025-06-10T20:49:59","date_gmt":"2025-06-10T12:49:59","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/php\/php-application-manual\/jcyy\/465.html"},"modified":"2025-06-10T20:49:59","modified_gmt":"2025-06-10T12:49:59","slug":"php%ef%bc%9a%e4%bb%8e%e5%87%ba%e7%94%9f%e6%97%a5%e6%9c%9f%e8%ae%a1%e7%ae%97%e5%b9%b4%e9%be%84","status":"publish","type":"my1js","link":"https:\/\/www.zhaozhao123.cn\/php\/my1js\/465.html","title":{"rendered":"PHP\uff1a\u4ece\u51fa\u751f\u65e5\u671f\u8ba1\u7b97\u5e74\u9f84"},"content":{"rendered":"<p><body><\/p>\n<h2>&#20171;&#32461;<\/h2>\n<p>&#24403;&#28041;&#21450;&#21040;&#22312;&#24212;&#29992;&#31243;&#24207;&#20013;&#31649;&#29702;&#20010;&#20154;&#25968;&#25454;&#26102;&#65292;&#35745;&#31639;&#24180;&#40836;&#26159;&#19968;&#39033;&#24120;&#35265;&#30340;&#25805;&#20316;&#12290;&#22312;PHP&#20013;&#65292;&#36825;&#39033;&#20219;&#21153;&#28041;&#21450;&#22788;&#29702;&#26085;&#26399;&#24182;&#29702;&#35299;DateTime&#31867;&#12290;&#35753;&#25105;&#20204;&#25506;&#35752;&#21508;&#31181;&#20934;&#30830;&#35745;&#31639;&#24180;&#40836;&#30340;&#26041;&#27861;&#12290;<\/p>\n<h2>&#22522;&#26412;&#30340;&#26085;&#26399;&#26102;&#38388;&#35745;&#31639;<\/h2>\n<p>&#39318;&#20808;&#65292;&#20351;&#29992;DateTime&#31867;&#26469;&#35745;&#31639;PHP&#20013;&#30340;&#24180;&#40836;&#12290;&#36825;&#31181;&#38754;&#21521;&#23545;&#35937;&#30340;&#26041;&#27861;&#25552;&#20379;&#20102;&#19968;&#31181;&#21487;&#38752;&#30340;&#26041;&#24335;&#26469;&#22788;&#29702;&#26085;&#26399;&#65306;<\/p>\n<pre><code>$birthdate = new DateTime('1990-01-01');\n$today = new DateTime('now');\n$interval = $today-&gt;diff($birthdate);\necho $interval-&gt;y;<\/code><\/pre>\n<p>&#22312;&#35813;&#31034;&#20363;&#20013;&#65292;&#21019;&#24314;&#20102;&#19968;&#20010;DateTime&#23545;&#35937;&#26469;&#34920;&#31034;&#20986;&#29983;&#26085;&#26399;&#65292;&#21478;&#19968;&#20010;&#29992;&#20110;&#24403;&#21069;&#26085;&#26399;&#12290;&#20351;&#29992;diff&#26041;&#27861;&#35745;&#31639;&#24046;&#24322;&#65292;&#8217;y&#8217;&#23646;&#24615;&#21017;&#32473;&#20986;&#24635;&#24180;&#25968;&#65292;&#21363;&#24180;&#40836;&#12290;<\/p>\n<h2>&#22788;&#29702;&#38384;&#24180;<\/h2>\n<p>&#38384;&#24180;&#22686;&#21152;&#20102;&#24180;&#40836;&#35745;&#31639;&#30340;&#22797;&#26434;&#24615;&#12290;&#22312;&#20351;&#29992;DateTime&#23545;&#35937;&#30340;diff&#26041;&#27861;&#26102;&#65292;PHP&#20250;&#33258;&#21160;&#22788;&#29702;&#38384;&#24180;&#38382;&#39064;&#65306;<\/p>\n<pre><code>$birthdate = new DateTime('2004-02-29');\n$today = new DateTime('now');\n$interval = $today-&gt;diff($birthdate);\necho $interval-&gt;y;<\/code><\/pre>\n<p>&#23545;&#20110;&#20108;&#26376;&#20108;&#21313;&#20061;&#26085;&#20986;&#29983;&#30340;&#26085;&#26399;&#65292;&#24180;&#40836;&#35745;&#31639;&#26102;&#19981;&#38656;&#35201;&#39069;&#22806;&#30340;&#36923;&#36753;&#12290;<\/p>\n<h2>&#20351;&#29992;&#26102;&#38388;&#25139;<\/h2>\n<p>&#21478;&#19968;&#31181;&#26041;&#27861;&#28041;&#21450;&#20351;&#29992;Unix&#26102;&#38388;&#25139;&#65292;&#23427;&#20174;1970&#24180;1&#26376;1&#26085;&#24320;&#22987;&#35745;&#31639;&#31186;&#25968;&#12290;&#36825;&#37324;&#26159;&#22914;&#20309;&#29992;&#23427;&#20204;&#36827;&#34892;&#24180;&#40836;&#35745;&#31639;&#30340;&#65306;<\/p>\n<pre><code>$birthdate = strtotime('1990-01-01');\n$age = floor((time() - $birthdate) \/ 31556926); \/\/ seconds in a year\necho $age;<\/code><\/pre>\n<p>&#36825;&#31181;&#26041;&#27861;&#26377;&#19968;&#20123;&#32570;&#28857;&#65292;&#27604;&#22914;&#27809;&#26377;&#32771;&#34385;&#21040;&#38384;&#31186;&#38382;&#39064;&#65292;&#20294;&#25552;&#20379;&#20102;&#19968;&#31181;&#24555;&#36895;&#31616;&#20415;&#30340;&#26041;&#27861;&#26469;&#20272;&#31639;&#24180;&#40836;&#12290;<\/p>\n<h2>&#19982;&#26102;&#21306;&#30456;&#20851;&#30340;&#24037;&#20316;<\/h2>\n<p>&#26102;&#21306;&#20250;&#24433;&#21709;&#24180;&#40836;&#35745;&#31639;&#30340;&#20934;&#30830;&#24615;&#12290;&#20351;&#29992;DateTimeZone&#30830;&#20445;&#26080;&#35770;&#29992;&#25143;&#22312;&#20309;&#22788;&#65292;&#32467;&#26524;&#37117;&#26159;&#27491;&#30830;&#30340;&#65306;<\/p>\n<pre><code>$timezone = new DateTimeZone('America\/New_York');\n$birthdate = new DateTime('1990-01-01', $timezone);\n$today = new DateTime('now', $timezone);\n$interval = $today-&gt;diff($birthdate);\necho $interval-&gt;y;<\/code><\/pre>\n<p>&#36890;&#36807;&#26126;&#30830;&#35774;&#32622;&#26102;&#21306;&#65292;&#24180;&#40836;&#35745;&#31639;&#20445;&#25345;&#19968;&#33268;&#19988;&#20934;&#30830;&#12290;<\/p>\n<h2>&#39640;&#32423;&#65306;&#19982;DateTimeImmutable&#30340;&#24037;&#20316;<\/h2>\n<p>&#26085;&#26399;&#26102;&#38388;&#31867;&#22411;&#21487;&#33021;&#20250;&#20986;&#29616;&#19968;&#20123;&#24847;&#24819;&#19981;&#21040;&#30340;&#34892;&#20026;&#65292;&#22240;&#27492;&#20026;&#20102;&#36991;&#20813;&#38382;&#39064;&#65292;&#35831;&#32771;&#34385;&#20351;&#29992;DateTimeImmutable&#65306;<\/p>\n<pre><code>$birthdate = new DateTimeImmutable('1990-01-01');\n$today = new DateTimeImmutable('now');\n$interval = $today-&gt;diff($birthdate);\necho $interval-&gt;y;<\/code><\/pre>\n<p>&#36825;&#20010;&#20195;&#30721;&#19982;&#25105;&#20204;&#31532;&#19968;&#20010;&#31034;&#20363;&#38750;&#24120;&#30456;&#20284;&#65307;&#28982;&#32780;&#65292;&#20351;&#29992;DateTimeImmutable&#30830;&#20445;&#23545;&#35937;&#30340;&#29366;&#24577;&#19981;&#20250;&#25913;&#21464;&#65292;&#20174;&#32780;&#20351;&#24212;&#29992;&#31243;&#24207;&#30340;&#36923;&#36753;&#26356;&#21152;&#21487;&#39044;&#27979;&#21644;&#23433;&#20840;&#12290;<\/p>\n<h2>&#33258;&#23450;&#20041;&#23454;&#29992;&#20989;&#25968;<\/h2>\n<p>&#20026;&#20102;&#23553;&#35013;&#25105;&#20204;&#30340;&#36923;&#36753;&#24182;&#23481;&#26131;&#37325;&#29992;&#65292;&#25105;&#20204;&#21487;&#20197;&#21019;&#24314;&#19968;&#20010;&#33258;&#23450;&#20041;&#20989;&#25968;&#26469;&#35745;&#31639;&#24180;&#40836;&#65306;<\/p>\n<pre><code>function calculateAge($birthdate) {\n    $date = new DateTime($birthdate);\n    $now = new DateTime();\n    $interval = $now-&gt;diff($date);\n    return $interval-&gt;y;\n}\necho calculateAge('1990-01-01');<\/code><\/pre>\n<p>&#35813;&#21151;&#33021;&#29616;&#22312;&#21487;&#20197;&#22312;&#39033;&#30446;&#20013;&#30340;&#20219;&#20309;&#22320;&#26041;&#35843;&#29992;&#65292;&#20174;&#32780;&#25913;&#21892;&#20195;&#30721;&#32452;&#32455;&#21644;&#21487;&#35835;&#24615;&#12290;<\/p>\n<h2>&#32467;&#35770;&#12290;<\/h2>\n<p>&#32463;&#36807;&#20174;&#22522;&#30784;&#21040;&#39640;&#32423;&#30340;&#23457;&#26597;&#65292;&#25105;&#20204;&#30475;&#21040;&#20102;PHP DateTime&#21644;DateTimeImmutable&#31867;&#25552;&#20379;&#30340;&#24378;&#22823;&#19988;&#28789;&#27963;&#30340;&#24037;&#20855;&#65292;&#29992;&#20110;&#24180;&#40836;&#35745;&#31639;&#12290;&#35831;&#21153;&#24517;&#32771;&#34385;&#38384;&#24180;&#12289;&#26102;&#21306;&#20197;&#21450;&#21487;&#36991;&#20813;&#24212;&#29992;&#31243;&#24207;&#20013;&#30340;bug&#30340;&#21487;&#21464;&#24615;&#12290;&#26377;&#20102;&#36825;&#20123;&#25552;&#31034;&#65292;&#20320;&#29616;&#22312;&#21487;&#20197;&#20026;&#24744;&#30340;PHP&#39033;&#30446;&#23454;&#26045;&#31283;&#20581;&#30340;&#24180;&#40836;&#35745;&#31639;&#12290;<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#20171;&#32461; &#24403;&#28041;&#21450;&#21040;&#22312;&#24212;&#29992;&#31243;&#24207;&#20013;&#31649;&#29702;&#20010;&#20154;&#25968;&#25454;&#26102;&#038;#65292..<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":false},"tags":[],"my1js2nav":[38],"tuisongtax":[],"class_list":["post-465","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\/465","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=465"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tags?post=465"},{"taxonomy":"my1js2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js2nav?post=465"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tuisongtax?post=465"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}