{"id":463,"date":"2025-06-10T20:48:26","date_gmt":"2025-06-10T12:48:26","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/php\/php-application-manual\/jcyy\/463.html"},"modified":"2025-06-10T20:48:26","modified_gmt":"2025-06-10T12:48:26","slug":"php%ef%bc%9a%e5%a6%82%e4%bd%95%e5%af%b9%e6%97%a5%e6%9c%9f%e6%95%b0%e7%bb%84%e8%bf%9b%e8%a1%8c%e9%99%8d%e5%ba%8f-%e5%8d%87%e5%ba%8f%e6%8e%92%e5%ba%8f","status":"publish","type":"my1js","link":"https:\/\/www.zhaozhao123.cn\/php\/my1js\/463.html","title":{"rendered":"PHP\uff1a\u5982\u4f55\u5bf9\u65e5\u671f\u6570\u7ec4\u8fdb\u884c\u964d\u5e8f\/\u5347\u5e8f\u6392\u5e8f"},"content":{"rendered":"<p><body><\/p>\n<h2>&#20171;&#32461;<\/h2>\n<p>&#25490;&#24207;&#25968;&#32452;&#26159;&#32534;&#31243;&#20013;&#30340;&#22522;&#26412;&#20219;&#21153;&#20043;&#19968;&#65292;&#32780;&#24403;&#28041;&#21450;&#21040;&#26085;&#26399;&#25490;&#24207;&#26102;&#65292;PHP &#25552;&#20379;&#20102;&#22810;&#31181;&#20989;&#25968;&#26469;&#31616;&#21270;&#36825;&#20010;&#36807;&#31243;&#12290;&#26080;&#35770;&#21319;&#24207;&#36824;&#26159;&#38477;&#24207;&#65292;&#27491;&#30830;&#22320;&#23545;&#26085;&#26399;&#36827;&#34892;&#25490;&#24207;&#23545;&#20110;&#26102;&#38388;&#30456;&#20851;&#25968;&#25454;&#30340;&#22788;&#29702;&#33267;&#20851;&#37325;&#35201;&#12290;<\/p>\n<h2>&#20351;&#29992; usort &#19982; strtotime<\/h2>\n<p>&#20026;&#20102;&#24320;&#22987;&#23545;&#26085;&#26399;&#25968;&#32452;&#36827;&#34892;&#25490;&#24207;&#65292;&#20320;&#21487;&#20197;&#20351;&#29992;<code>usort<\/code>&#19982;&#20043;&#30456;&#32467;&#21512;&#30340;<code>strtotime<\/code>&#21151;&#33021;&#12290;&#36825;&#31181;&#26041;&#27861;&#20801;&#35768;&#36827;&#34892;&#33258;&#23450;&#20041;&#25490;&#24207;&#36923;&#36753;&#65292;&#19988;&#21487;&#20197;&#36731;&#26494;&#36866;&#24212;&#21319;&#24207;&#21644;&#38477;&#24207;&#20004;&#31181;&#39034;&#24207;&#12290;<\/p>\n<pre><code>\n$dates = array(\n    '2023-03-25',\n    '2023-02-15',\n    '2023-04-01'\n);\n\nusort($dates, function($a, $b) {\n    return strtotime($a) - strtotime($b);\n});\n\nprint_r($dates); \/\/ Output will be in ascending order\n<\/code><\/pre>\n<p>&#20026;&#20102;&#25353;&#38477;&#24207;&#25490;&#24207;&#65292;&#21482;&#38656;&#22312;&#27604;&#36739;&#20989;&#25968;&#20013;&#39072;&#20498;&#20943;&#27861;&#21363;&#21487;&#12290;<\/p>\n<pre><code>\nusort($dates, function($a, $b) {\n    return strtotime($b) - strtotime($a);\n});\n\nprint_r($dates); \/\/ Output will be in descending order\n<\/code><\/pre>\n<h2>&#25490;&#24207;&#26102;&#20351;&#29992;DateTime&#31867;<\/h2>\n<p>&#21478;&#19968;&#31181;&#26041;&#27861;&#28041;&#21450;DateTime&#31867;&#65292;&#23427;&#25552;&#20379;&#20102;&#26356;&#23545;&#35937;&#21270;&#30340;&#22788;&#29702;&#26085;&#26399;&#21644;&#25490;&#24207;&#30340;&#26041;&#24335;&#12290;<\/p>\n<pre><code>\n$dates = array(\n    new DateTime('2023-03-25'),\n    new DateTime('2023-02-15'),\n    new DateTime('2023-04-01')\n);\n\nusort($dates, function($a, $b) {\n    return $a &lt;=&gt; $b;\n});\n\nforeach ($dates as $date) {\n    echo $date-&gt;format('Y-m-d') . '\\n';\n} \/\/ Outputs ascending order\n<\/code><\/pre>\n<p>&#20351;&#29992;&#26143;&#21495;&#25805;&#20316;&#31526;&#12290;<code>&lt;=&gt;<\/code>&#20026;&#20102;&#25972;&#29702;&#29289;&#21697;&#12290;&#35201;&#25353;&#30456;&#21453;&#39034;&#24207;&#25972;&#29702;&#65292;&#21482;&#38656;&#32763;&#36716;&#21363;&#21487;&#12290;<code>$a<\/code>and &#26159;&ldquo;&#24182;&#19988;&rdquo;&#30340;&#24847;&#24605;&#12290;<code>$b<\/code>&#22909;&#30340;&#65292;&#35831;&#21457;&#36865;&#20320;&#38656;&#35201;&#32763;&#35793;&#30340;&#20869;&#23481;&#12290;<\/p>\n<pre><code>\nusort($dates, function($a, $b) {\n    return $b &lt;=&gt; $a;\n});\n\nforeach ($dates as $date) {\n    echo $date-&gt;format('Y-m-d') . '\\n';\n} \/\/ Outputs descending order\n<\/code><\/pre>\n<h2>&#25968;&#32452;&#22810;&#25490;&#24207;&#65292;&#25552;&#21462;&#26102;&#38388;&#25139;<\/h2>\n<p>&#23545;&#19981;&#36215;&#65292;&#24744;&#30340;&#28040;&#24687;&#27809;&#26377;&#23436;&#20840;&#26174;&#31034;&#20986;&#26469;&#12290;&#35831;&#37325;&#26032;&#21457;&#36865;&#25110;&#25552;&#20379;&#26356;&#22810;&#32454;&#33410;&#12290;<code>array_multisort<\/code>&#35813;&#20989;&#25968;&#21487;&#20197;&#21516;&#26102;&#23545;&#22810;&#20010;&#25968;&#32452;&#25110;&#22810;&#32500;&#25968;&#32452;&#36827;&#34892;&#25490;&#24207;&#12290;&#36890;&#36807;&#20174;&#26085;&#26399;&#20013;&#25552;&#21462;&#26102;&#38388;&#25139;&#65292;&#20320;&#21487;&#20197;&#20351;&#29992;&#23427;&#26469;&#36827;&#34892;&#25490;&#24207;&#12290;<code>array_multisort<\/code>&#28857;&#20182;&#20204;&#12290;<\/p>\n<pre><code>\n$dates = array(\n    '2023-03-25',\n    '2023-02-15',\n    '2023-04-01'\n);\n$timestamps = array_map('strtotime', $dates);\n\narray_multisort($timestamps, SORT_ASC, $dates);\n\nprint_r($dates); \/\/ Output will be in ascending order\n\n\/\/ For descending order, change SORT_ASC to SORT_DESC\narray_multisort($timestamps, SORT_DESC, $dates);\n\nprint_r($dates); \/\/ Output will be in descending order\n<\/code><\/pre>\n<h2>&#19982;&#25968;&#25454;&#24211;&#26597;&#35810;&#38598;&#25104;<\/h2>\n<p>&#25490;&#24207;&#20063;&#21487;&#20197;&#30452;&#25509;&#22312;&#25968;&#25454;&#24211;&#26597;&#35810;&#20013;&#36827;&#34892;&#65292;&#20174;&#32780;&#20248;&#21270;&#24615;&#33021;&#12290;&#20351;&#29992;SQL&#30340;&#25490;&#24207;&#21151;&#33021;&#12290;<code>ORDER BY<\/code>&#22312;&#23558;&#26085;&#26399;&#28155;&#21152;&#21040;&#25968;&#32452;&#20043;&#21069;&#65292;&#23601;&#23545;&#20854;&#36827;&#34892;&#25490;&#24207;&#12290;<\/p>\n<pre><code>\n\/\/ Using PDO for a MySQL query\n$pdo = new PDO('mysql:host=localhost;dbname=your_db', 'user', 'password');\n$stmt = $pdo-&gt;query('SELECT * FROM your_table ORDER BY your_date_column ASC');\n\n$dates = $stmt-&gt;fetchAll(PDO::FETCH_ASSOC);\n\n\/\/ The $dates array is now sorted in ascending order\n<\/code><\/pre>\n<p>&#36825;&#31181;&#26041;&#27861;&#29305;&#21035;&#36866;&#29992;&#20110;&#22823;&#25968;&#25454;&#38598;&#65292;&#22240;&#20026;PHP&#32423;&#21035;&#30340;&#25490;&#24207;&#21487;&#33021;&#20250;&#21464;&#24471;&#19981;&#39640;&#25928;&#12290;<\/p>\n<h2>&#24615;&#33021;&#32771;&#34385;&#22240;&#32032;<\/h2>\n<p>&#22312;&#22788;&#29702;&#22823;&#22411;&#25968;&#32452;&#26102;&#65292;&#37325;&#35201;&#30340;&#26159;&#35201;&#32771;&#34385;&#25490;&#24207;&#31639;&#27861;&#30340;&#24615;&#33021;&#12290;&#20363;&#22914;&#65292;&#24403;&#20351;&#29992;&#26576;&#20123;&#20989;&#25968;&#36827;&#34892;&#25490;&#24207;&#26102;&#65292;&#38656;&#35201;&#32771;&#34385;&#23427;&#20204;&#30340;&#34920;&#29616;&#12290;<code>usort<\/code>&#23545;&#19981;&#36215;&#65292;&#25105;&#26080;&#27861;&#29702;&#35299;&#36825;&#21477;&#35805;&#30340;&#24847;&#24605;&#12290;<code>strtotime<\/code>&#23545;&#20110;&#36739;&#22823;&#30340;&#25968;&#25454;&#38598;&#65292;&#39044;&#35745;&#31639;&#26102;&#38388;&#25139;&#25110;&#22312;&#25968;&#25454;&#24211;&#20869;&#25490;&#24207;&#21487;&#33021;&#25552;&#20379;&#26356;&#22909;&#30340;&#24615;&#33021;&#12290;<\/p>\n<h2>&#32467;&#35770;&#12290;<\/h2>\n<p>&#32508;&#19978;&#25152;&#36848;&#65292;PHP &#25552;&#20379;&#20102;&#19968;&#22871;&#24378;&#22823;&#30340;&#20989;&#25968;&#26469;&#23545;&#26085;&#26399;&#25968;&#32452;&#36827;&#34892;&#21319;&#24207;&#25110;&#38477;&#24207;&#25490;&#24207;&#12290;&#20320;&#21487;&#20197;&#36890;&#36807;&#23454;&#39564;&#19981;&#21516;&#30340;&#26041;&#27861;&#25214;&#21040;&#26368;&#36866;&#21512;&#20320;&#29305;&#23450;&#29992;&#20363;&#30340;&#24179;&#34913;&#28857;&#12290;&#22312;&#19981;&#26029;&#32451;&#20064;&#20043;&#21518;&#65292;&#22788;&#29702;&#26085;&#26399;&#25490;&#24207;&#23558;&#22312;&#20320;&#30340; PHP &#24320;&#21457;&#26053;&#31243;&#20013;&#21464;&#24471;&#22914;&#40060;&#24471;&#27700;&#12290;<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#20171;&#32461; &#25490;&#24207;&#25968;&#32452;&#26159;&#32534;&#31243;&#20013;&#30340;&#22522;&#26412;&#20219;&#21153;&#20043;&#19968;&#65292;&#32780;&#038;#24403..<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":false},"tags":[],"my1js2nav":[38],"tuisongtax":[],"class_list":["post-463","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\/463","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=463"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tags?post=463"},{"taxonomy":"my1js2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js2nav?post=463"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tuisongtax?post=463"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}