{"id":457,"date":"2025-06-10T20:43:07","date_gmt":"2025-06-10T12:43:07","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/php\/php-application-manual\/jcyy\/457.html"},"modified":"2025-06-10T20:43:07","modified_gmt":"2025-06-10T12:43:07","slug":"php%ef%bc%9a%e5%b0%86%e6%97%a5%e6%9c%9f%e6%97%b6%e9%97%b4%e8%bd%ac%e6%8d%a2%e4%b8%ba%e6%97%b6%e9%97%b4%e5%89%8d%e6%a0%bc%e5%bc%8f%e5%8f%8a%e5%8f%8d%e5%90%91%e8%bd%ac%e6%8d%a2","status":"publish","type":"my1js","link":"https:\/\/www.zhaozhao123.cn\/php\/my1js\/457.html","title":{"rendered":"PHP\uff1a\u5c06\u65e5\u671f\u65f6\u95f4\u8f6c\u6362\u4e3a\u201c\u65f6\u95f4\u524d\u201d\u683c\u5f0f\u53ca\u53cd\u5411\u8f6c\u6362"},"content":{"rendered":"<p><body><\/p>\n<h2>&#20171;&#32461;<\/h2>\n<p>&#22788;&#29702;&#26085;&#26399;&#21644;&#26102;&#38388;&#22312;&#35768;&#22810;PHP&#24212;&#29992;&#20013;&#37117;&#26159;&#24120;&#35265;&#38656;&#27714;&#12290;&#26080;&#35770;&#26159;&#24320;&#21457;&#31038;&#20132;&#32593;&#32476;&#12289;&#21338;&#23458;&#24179;&#21488;&#36824;&#26159;&#20010;&#20154;&#39033;&#30446;&#65292;&#26174;&#31034;&#30456;&#23545;&#26102;&#38388;&#65288;&#22914;&ldquo;2&#20998;&#38047;&#21069;&rdquo;&#25110;&#23558;&#27492;&#31867;&#30701;&#35821;&#36716;&#25442;&#22238;&#26631;&#20934;&#21270;&#30340;&#26085;&#26399;&#26102;&#38388;&#65289;&#23545;&#20110;&#25552;&#21319;&#29992;&#25143;&#20307;&#39564;&#38750;&#24120;&#26377;&#29992;&#12290;&#26412;&#25945;&#31243;&#23558;&#25351;&#23548;&#24744;&#36890;&#36807;&#21508;&#31181;PHP&#20989;&#25968;&#21644;&#25216;&#26415;&#36731;&#26494;&#23454;&#29616;&#36825;&#20123;&#36716;&#25442;&#12290;<\/p>\n<h2>&#23558;&#26085;&#26399;&#26102;&#38388;&#36716;&#25442;&#20026;&#36317;&#20170;&#22810;&#20037;&#12290;<\/h2>\n<p>&#23558;&#29305;&#23450;&#26085;&#26399;&#21644;&#26102;&#38388;&#36716;&#25442;&#20026;&ldquo;&#22810;&#38271;&#26102;&#38388;&#20197;&#21069;&rdquo;&#30340;&#26684;&#24335;&#65292;&#20363;&#22914;&ldquo;&#31186;&#20197;&#21069;&rdquo;&#12289;&ldquo;&#20998;&#38047;&#20197;&#21069;&rdquo;&#31561;&#65292;&#21487;&#20197;&#35753;&#29992;&#25143;&#24863;&#21463;&#21040;&#20107;&#20214;&#21457;&#29983;&#30340;&#26102;&#38388;&#36317;&#31163;&#12290;&#20197;&#19979;&#26159;&#19968;&#20010;&#31034;&#20363;&#65292;&#23637;&#31034;&#22914;&#20309;&#21019;&#24314;&#19968;&#20010;&#31616;&#21333;&#30340;PHP&#20989;&#25968;&#26469;&#25191;&#34892;&#27492;&#36716;&#25442;&#65306;<\/p>\n<pre><code>function timeAgo($datetime) {\n    $time = strtotime($datetime);\n    $diff = time() - $time;\n\n    if ($diff &lt; 60) {\n        return 'Just now';\n    }\n\n    $timeRules = [\n        ['minute', 60],\n        ['hour', 3600],\n        ['day', 86400],\n        ['week', 604800],\n        ['month', 2592000],\n        ['year', 31536000],\n    ];\n\n    foreach ($timeRules as $rule) {\n        $divided = $diff \/ $rule[1];\n        if ($divided &gt;= 1) {\n            $r = round($divided);\n            return $r . ' ' . ($r &gt; 1 ? $rule[0].'s' : $rule[0]) . ' ago';\n        }\n    }\n}\n<\/code><\/pre>\n<p>&#36825;&#20010;&#21151;&#33021;&#25509;&#21463;&#19968;&#20010;&#26085;&#26399;&#26102;&#38388;&#23383;&#31526;&#20018;&#20316;&#20026;&#36755;&#20837;&#65292;&#35745;&#31639;&#24403;&#21069;&#26102;&#38388;&#21644;&#25552;&#20379;&#30340;&#26102;&#38388;&#20043;&#38388;&#30340;&#24046;&#20540;&#65292;&#24182;&#20197;&#21487;&#35835;&#30340;&ldquo;&#22810;&#38271;&#26102;&#38388;&#20197;&#21069;&rdquo;&#26684;&#24335;&#26174;&#31034;&#12290;<\/p>\n<h2>&#23558;&ldquo;Time Ago&rdquo;&#36716;&#25442;&#20026;&#26085;&#26399;&#26102;&#38388;&#12290;<\/h2>\n<p>&#22312;&#26174;&#31034;&ldquo;&#19968;&#27573;&#26102;&#38388;&#21069;&rdquo;&#24456;&#26377;&#24110;&#21161;&#65292;&#20294;&#22312;&#26576;&#20123;&#24773;&#20917;&#19979;&#65292;&#20320;&#38656;&#35201;&#23558;&#20854;&#36716;&#25442;&#22238;&#26631;&#20934;&#26085;&#26399;&#21644;&#26102;&#38388;&#26684;&#24335;&#12290;&#20197;&#19979;&#31034;&#20363;&#23637;&#31034;&#20102;&#36825;&#19968;&#36716;&#25442;&#36807;&#31243;&#65306;<\/p>\n<pre><code>function timeAgoToDate($timeAgo) {\n    $currentTime = time();\n    $timeAgo = strtolower($timeAgo);\n\n    $deconstructed = sscanf($timeAgo, '%d %s ago', $value, $unit);\n    if($deconstructed &lt; 2) {\n        \/\/ Handle error\n        return null;\n    }\n\n    $unitsToSeconds = [\n        'second' =&gt; 1,\n        'minute' =&gt; 60,\n        'hour'   =&gt; 3600,\n        'day'    =&gt; 86400,\n        'week'   =&gt; 604800,\n        'month'  =&gt; 2592000,\n        'year'   =&gt; 31536000,\n    ];\n\n    if (isset($unitsToSeconds[$unit]) || isset($unitsToSeconds[$unit.'s'])) {\n        $unit = rtrim($unit, 's');\n        $secondsAgo = $value * $unitsToSeconds[$unit];\n        $date = date('Y-m-d H:i:s', $currentTime - $secondsAgo);\n        return $date;\n    }    \n\n    return null;\n}\n<\/code><\/pre>\n<p>&#22312;&#35813;&#21151;&#33021;&#20013;&#65292;&#25105;&#20204;&#23558;&#31867;&#20284;&#20110;&ldquo;3&#22825;&#21069;&rdquo;&#30340;&#23383;&#31526;&#20018;&#25552;&#21462;&#20986;&#25968;&#23383;&#20540;&#21644;&#26102;&#38388;&#21333;&#20301;&#65292;&#28982;&#21518;&#20174;&#24403;&#21069;&#26102;&#38388;&#20943;&#21435;&#30456;&#24212;&#30340;&#26102;&#38388;&#26469;&#33719;&#21462;&#21407;&#22987;&#26085;&#26399;&#21644;&#26102;&#38388;&#12290;<\/p>\n<h2>&#21033;&#29992;DateTime&#31867;<\/h2>\n<p>PHP&#30340;DateTime&#31867;&#25552;&#20379;&#20102;&#26085;&#26399;&#21644;&#26102;&#38388;&#25805;&#20316;&#30340;&#23545;&#35937;&#21270;&#26041;&#24335;&#12290;&#20197;&#19979;&#26159;&#19968;&#20010;&#20351;&#29992;DateTime&#36716;&#25442;&#26085;&#26399;&#26102;&#38388;&#21040;&#36807;&#21435;&#22810;&#20037;&#21644;&#36807;&#21435;&#22810;&#20037;&#21040;DateTime&#30340;&#20363;&#23376;&#65306;<\/p>\n<pre><code>\/\/ DateTime to Time Ago\nfunction dateTimeToTimeAgo(Datetime $datetime) {\n    \/\/ Code similar to the above timeAgo function\n}\n\n\/\/ Time Ago to DateTime\nfunction timeAgoToDateTime($timeAgo) {\n    \/\/ Code similar to the above timeAgoToDate function\n}\n<\/code><\/pre>\n<p>&#20351;&#29992;DateTime&#31867;&#21487;&#20197;&#25552;&#20379;&#26356;&#22810;&#28789;&#27963;&#24615;&#12289;&#26102;&#21306;&#25903;&#25345;&#20197;&#21450;&#36890;&#36807;&#20869;&#32622;&#26041;&#27861;&#23454;&#29616;&#26356;&#22909;&#30340;&#38169;&#35823;&#22788;&#29702;&#12290;<\/p>\n<h2>&#22788;&#29702;&#26412;&#22320;&#21270;&#21644;&#26102;&#21306;&#38382;&#39064;<\/h2>\n<p>&#22788;&#29702;&#26469;&#33258;&#19981;&#21516;&#22320;&#21306;&#30340;&#29992;&#25143;&#38656;&#35201;&#32771;&#34385;&#22312;&#36716;&#25442;&#26102;&#38388;&#26102;&#36827;&#34892;&#26412;&#22320;&#21270;&#21644;&#26102;&#21306;&#30340;&#32771;&#37327;&#12290;&#25105;&#20204;&#21487;&#20197;&#20351;&#29992;DateTimeZone&#21644;IntlDateFormatter&#26469;&#35299;&#20915;&#36825;&#20123;&#38382;&#39064;&#65306;<\/p>\n<pre><code>\/\/ Convert to localized time ago\nfunction localizedTimeAgo(DateTime $datetime, $locale) {\n    \/\/ Localization example\n}\n\n\/\/ Convert localized time ago back to DateTime\nfunction localizedTimeAgoToDateTime($timeAgo, $timezone) {\n    \/\/ Conversion example, with time zone support\n}\n<\/code><\/pre>\n<p>&#26412;&#39640;&#32423;&#37096;&#20998;&#28436;&#31034;&#20102;&#22914;&#20309;&#20026;&#25105;&#20204;&#30340;&#26089;&#26399;&#21151;&#33021;&#36827;&#34892;&#23450;&#21046;&#65292;&#20197;&#32771;&#34385;&#21508;&#31181;&#35821;&#35328;&#21644;&#26102;&#21306;&#12290;<\/p>\n<h2>&#32467;&#35770;&#12290;<\/h2>\n<p>&#26102;&#38388;&#30456;&#20851;&#30340;&#36716;&#25442;&#22312;&#21019;&#24314;&#21451;&#22909;&#30340;&#29992;&#25143;&#20307;&#39564;&#26041;&#38754;&#23545;&#20110;PHP&#24212;&#29992;&#31243;&#24207;&#33267;&#20851;&#37325;&#35201;&#12290;&#25105;&#20204;&#24050;&#32463;&#25506;&#35752;&#20102;&#20174;&#22522;&#26412;&#21040;&#39640;&#32423;&#30340;&#26085;&#26399;&#21644;&#26102;&#38388;&#26684;&#24335;&#21270;&#25216;&#26415;&#65292;&#20197;&#21450;&#30456;&#21453;&#26041;&#21521;&#30340;&#36716;&#25442;&#26041;&#27861;&#12290;&#36890;&#36807;&#29702;&#35299;PHP&#30340;&#26085;&#26399;&#21644;&#26102;&#38388;&#20989;&#25968;&#65292;&#20197;&#21450;&#23454;&#26045;&#29616;&#20195;&#38754;&#21521;&#23545;&#35937;&#30340;&#27010;&#24565;&#22914;DateTime&#21644;DateTimeZone&#31867;&#65292;&#24744;&#21487;&#20197;&#36731;&#26494;&#22788;&#29702;&#39033;&#30446;&#20013;&#30340;&#26102;&#38388;&#25805;&#20316;&#12290;<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#20171;&#32461; &#22788;&#29702;&#26085;&#26399;&#21644;&#26102;&#38388;&#22312;&#35768;&#22810;PHP&#24212;&#29992;&#20013;&#37117;&#26159;&#24120;&#35265;&#038;#38..<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":false},"tags":[],"my1js2nav":[38],"tuisongtax":[],"class_list":["post-457","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\/457","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=457"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tags?post=457"},{"taxonomy":"my1js2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js2nav?post=457"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tuisongtax?post=457"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}