{"id":757,"date":"2025-01-05T22:32:02","date_gmt":"2025-01-05T14:32:02","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=757"},"modified":"2025-01-05T22:32:02","modified_gmt":"2025-01-05T14:32:02","slug":"%e6%80%8e%e6%a0%b7%e5%af%b9php-%e4%b8%ad%e7%9a%84%e6%97%a5%e6%9c%9f%e6%a0%bc%e5%bc%8f%e5%81%9a%e8%bd%ac%e6%8d%a2%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/05\/%e6%80%8e%e6%a0%b7%e5%af%b9php-%e4%b8%ad%e7%9a%84%e6%97%a5%e6%9c%9f%e6%a0%bc%e5%bc%8f%e5%81%9a%e8%bd%ac%e6%8d%a2%ef%bc%9f\/","title":{"rendered":"\u600e\u6837\u5bf9PHP \u4e2d\u7684\u65e5\u671f\u683c\u5f0f\u505a\u8f6c\u6362\uff1f"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u5728 PHP \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>DateTime<\/code> \u7c7b\u6216\u8005 <code>date()<\/code> \u51fd\u6570\u6765\u5c06\u65e5\u671f\u683c\u5f0f\u4ece <code>YYYY-MM-DD<\/code> \u8f6c\u6362\u4e3a <code>DD-MM-YYYY<\/code> \u683c\u5f0f\u3002\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e2a\u8f6c\u6362\u3002<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u4f7f\u7528 <code>DateTime<\/code> \u7c7b<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>DateTime<\/code> \u7c7b\u63d0\u4f9b\u4e86\u975e\u5e38\u65b9\u4fbf\u7684\u65e5\u671f\u5904\u7406\u529f\u80fd\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u8fdb\u884c\u65e5\u671f\u683c\u5f0f\u7684\u8f6c\u6362\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/\/ \u539f\u59cb\u65e5\u671f\u5b57\u7b26\u4e32\n$date = \"2025-01-05\";\n\n\/\/ \u521b\u5efa DateTime \u5bf9\u8c61\n$dateObj = DateTime::createFromFormat('Y-m-d', $date);\n\n\/\/ \u8f6c\u6362\u4e3a DD-MM-YYYY \u683c\u5f0f\n$formattedDate = $dateObj-&gt;format('d-m-Y');\n\necho $formattedDate;  \/\/ \u8f93\u51fa: 05-01-2025\n?&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u91ca\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>createFromFormat('Y-m-d', $date)<\/code>\uff1a\u6307\u5b9a\u8f93\u5165\u7684\u65e5\u671f\u683c\u5f0f\u4e3a <code>YYYY-MM-DD<\/code>\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a <code>DateTime<\/code> \u5bf9\u8c61\u3002<\/li>\n\n\n\n<li><code>format('d-m-Y')<\/code>\uff1a\u5c06 <code>DateTime<\/code> \u5bf9\u8c61\u683c\u5f0f\u5316\u4e3a <code>DD-MM-YYYY<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u4f7f\u7528 <code>date()<\/code> \u51fd\u6570<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u5df2\u7ecf\u62e5\u6709\u4e00\u4e2a\u7b26\u5408 <code>YYYY-MM-DD<\/code> \u683c\u5f0f\u7684\u65e5\u671f\u5b57\u7b26\u4e32\uff0c\u5e76\u4e14\u5e0c\u671b\u76f4\u63a5\u4f7f\u7528 <code>date()<\/code> \u51fd\u6570\u6765\u8f6c\u6362\uff0c\u4f60\u53ef\u4ee5\u5c06\u8be5\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233\uff0c\u7136\u540e\u518d\u683c\u5f0f\u5316\u8f93\u51fa\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/\/ \u539f\u59cb\u65e5\u671f\u5b57\u7b26\u4e32\n$date = \"2025-01-05\";\n\n\/\/ \u5c06\u65e5\u671f\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233\n$timestamp = strtotime($date);\n\n\/\/ \u4f7f\u7528 date() \u51fd\u6570\u8f6c\u6362\u4e3a DD-MM-YYYY \u683c\u5f0f\n$formattedDate = date('d-m-Y', $timestamp);\n\necho $formattedDate;  \/\/ \u8f93\u51fa: 05-01-2025\n?&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u91ca\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>strtotime($date)<\/code>\uff1a\u5c06\u65e5\u671f\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233\u3002<\/li>\n\n\n\n<li><code>date('d-m-Y', $timestamp)<\/code>\uff1a\u4f7f\u7528 <code>date()<\/code> \u51fd\u6570\u683c\u5f0f\u5316\u65f6\u95f4\u6233\u4e3a <code>DD-MM-YYYY<\/code> \u683c\u5f0f\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u4f7f\u7528 <code>explode()<\/code> \u548c\u5b57\u7b26\u4e32\u64cd\u4f5c<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4e0d\u60f3\u4f7f\u7528 <code>DateTime<\/code> \u7c7b\u6216\u8005 <code>strtotime()<\/code> \u51fd\u6570\uff0c\u4f60\u4e5f\u53ef\u4ee5\u76f4\u63a5\u64cd\u4f5c\u5b57\u7b26\u4e32\uff0c\u5c06\u65e5\u671f\u62c6\u5206\u5e76\u91cd\u65b0\u6392\u5217\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/\/ \u539f\u59cb\u65e5\u671f\u5b57\u7b26\u4e32\n$date = \"2025-01-05\";\n\n\/\/ \u4f7f\u7528 explode() \u62c6\u5206\u5b57\u7b26\u4e32\n$dateParts = explode('-', $date);\n\n\/\/ \u91cd\u65b0\u7ec4\u5408\u4e3a DD-MM-YYYY \u683c\u5f0f\n$formattedDate = $dateParts&#91;2] . '-' . $dateParts&#91;1] . '-' . $dateParts&#91;0];\n\necho $formattedDate;  \/\/ \u8f93\u51fa: 05-01-2025\n?&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u91ca\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>explode('-', $date)<\/code>\uff1a\u5c06\u539f\u59cb\u65e5\u671f\u5b57\u7b26\u4e32\u6309 <code>-<\/code> \u62c6\u5206\u4e3a\u4e00\u4e2a\u6570\u7ec4\uff0c\u6570\u7ec4\u7684\u5143\u7d20\u5206\u522b\u4e3a\u5e74\u3001\u6708\u3001\u65e5\u3002<\/li>\n\n\n\n<li>\u901a\u8fc7\u91cd\u65b0\u6392\u5217\u6570\u7ec4\u7684\u5143\u7d20\uff08<code>$dateParts[2]<\/code>, <code>$dateParts[1]<\/code>, <code>$dateParts[0]<\/code>\uff09\uff0c\u5c06\u65e5\u671f\u683c\u5f0f\u8f6c\u6362\u4e3a <code>DD-MM-YYYY<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 <code>DateTime<\/code> \u7c7b\u662f\u6700\u63a8\u8350\u7684\u65b9\u6cd5\uff0c\u8bed\u6cd5\u7b80\u6d01\u4e14\u529f\u80fd\u5f3a\u5927\u3002<\/li>\n\n\n\n<li><code>strtotime()<\/code> \u548c <code>date()<\/code> \u51fd\u6570\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u683c\u5f0f\u8f6c\u6362\uff0c\u9002\u7528\u4e8e\u7b80\u5355\u7684\u65e5\u671f\u683c\u5f0f\u5904\u7406\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u4e0d\u60f3\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5b57\u7b26\u4e32\u64cd\u4f5c\u6765\u5b9e\u73b0\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u63a8\u8350\u4f7f\u7528 <code>DateTime<\/code> \u7c7b\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u529f\u80fd\u5168\u9762\uff0c\u800c\u4e14\u80fd\u591f\u907f\u514d\u4e0e\u5176\u4ed6\u65e5\u671f\u683c\u5f0f\u5904\u7406\u76f8\u5173\u7684\u6f5c\u5728\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 PHP \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 DateTime \u7c7b\u6216\u8005 date() \u51fd\u6570\u6765\u5c06\u65e5\u671f\u683c\u5f0f\u4ece YYYY-MM-DD \u8f6c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61],"tags":[],"class_list":["post-757","post","type-post","status-publish","format-standard","hentry","category-php-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/757","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=757"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/757\/revisions"}],"predecessor-version":[{"id":758,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/757\/revisions\/758"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=757"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=757"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}