{"id":10008182,"date":"2023-08-28T18:00:00","date_gmt":"2023-08-28T10:00:00","guid":{"rendered":"https:\/\/blog.itmax.cn\/?p=10008182"},"modified":"2023-08-28T18:00:00","modified_gmt":"2023-08-28T10:00:00","slug":"php%e4%b8%ad%e7%9a%84%e6%97%a5%e6%9c%9f%e5%92%8c%e6%97%b6%e9%97%b4%e5%a4%84%e7%90%86%e5%87%bd%e6%95%b0%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10008182","title":{"rendered":"PHP\u4e2d\u7684\u65e5\u671f\u548c\u65f6\u95f4\u5904\u7406\u51fd\u6570\u8be6\u89e3"},"content":{"rendered":"<p>\u5728 Web \u5f00\u53d1\u4e2d\uff0c\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u4efb\u52a1\u3002PHP \u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u5f3a\u5927\u7684\u65e5\u671f\u548c\u65f6\u95f4\u5904\u7406\u51fd\u6570\uff0c\u4f8b\u5982<code>strtotime<\/code>\u3001<code>date<\/code>\u548c<code>DateTimeImmutable::createFromFormat<\/code>\u7b49\u3002<\/p>\n<p>\u8fd9\u4e9b\u51fd\u6570\u4f7f\u5f97\u5728\u4e0d\u540c\u7684\u65f6\u95f4\u683c\u5f0f\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\uff0c\u8fdb\u884c\u65e5\u671f\u548c\u65f6\u95f4\u8ba1\u7b97\u4ee5\u53ca\u683c\u5f0f\u5316\u8f93\u51fa\u53d8\u5f97\u66f4\u52a0\u4fbf\u6377\u3002<\/p>\n<p>\u672c\u6587\u5c06\u6df1\u5165\u63a2\u8ba8\u8fd9\u4e09\u4e2a\u51fd\u6570\u7684\u7528\u6cd5\u548c\u4f18\u52bf\u3002<\/p>\n<h2>1. strtotime \u51fd\u6570<\/h2>\n<p><code>strtotime<\/code>\u51fd\u6570\u7528\u4e8e\u5c06\u4eba\u7c7b\u53ef\u8bfb\u7684\u65e5\u671f\u548c\u65f6\u95f4\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a Unix \u65f6\u95f4\u6233\u3002\u5b83\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2a\u65e5\u671f\u65f6\u95f4\u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u5c1d\u8bd5\u89e3\u6790\u5b83\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684 Unix \u65f6\u95f4\u6233\u3002\u9664\u4e86\u63a5\u53d7\u57fa\u672c\u7684\u65e5\u671f\u65f6\u95f4\u683c\u5f0f\u5916\uff0c\u5b83\u8fd8\u53ef\u4ee5\u7406\u89e3\u5404\u79cd\u76f8\u5bf9\u65f6\u95f4\u8868\u8fbe\u5f0f\u3002\u4ee5\u4e0b\u662f<code>strtotime<\/code>\u51fd\u6570\u7684\u53c2\u6570\u548c\u4f5c\u7528\uff1a<\/p>\n<pre><code class=\"php\">strtotime(string $datetime, ?int $baseTimestamp = null): int|false<\/code><\/pre>\n<ul>\n<li>\u53c2\u6570\uff1a<code>string $datetime, ?int $baseTimestamp = null<\/code><\/li>\n<li><code>$datetime<\/code>\uff1a\u9700\u8981\u89e3\u6790\u7684\u65e5\u671f\u65f6\u95f4\u5b57\u7b26\u4e32\u3002<\/li>\n<li><code>$baseTimestamp<\/code>\uff1a\u53ef\u9009\u53c2\u6570\uff0c\u8868\u793a\u7528\u4e8e\u8ba1\u7b97\u76f8\u5bf9\u65e5\u671f\u7684\u57fa\u7840\u65f6\u95f4\u6233\u3002<\/li>\n<li>\u8fd4\u56de\u503c\uff1a\u89e3\u6790\u6210\u529f\u5219\u8fd4\u56de\u5bf9\u5e94\u7684 Unix \u65f6\u95f4\u6233\uff0c\u89e3\u6790\u5931\u8d25\u5219\u8fd4\u56de<code>false<\/code>\u3002<\/li>\n<\/ul>\n<p>\u4f7f\u7528<code>strtotime<\/code>\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u4f20\u9012\u5404\u79cd\u4e0d\u540c\u683c\u5f0f\u7684\u65e5\u671f\u65f6\u95f4\u5b57\u7b26\u4e32\uff0c\u5305\u62ec\u7edd\u5bf9\u65f6\u95f4\uff08\u5982&#8221;2023-08-06&#8243;\u3001&#8221;15:30:00&#8243;\uff09\u4ee5\u53ca\u76f8\u5bf9\u65f6\u95f4\uff08\u5982&#8221;tomorrow&#8221;\u3001&#8221;next week&#8221;\uff09\u3002<\/p>\n<p>\u51fd\u6570\u4f1a\u5c1d\u8bd5\u6839\u636e\u4f20\u5165\u7684\u5b57\u7b26\u4e32\u8fdb\u884c\u5408\u7406\u7684\u65e5\u671f\u65f6\u95f4\u8f6c\u6362\uff0c\u65b9\u4fbf\u8fdb\u884c\u65f6\u95f4\u7684\u8ba1\u7b97\u548c\u6bd4\u8f83\u3002<\/p>\n<pre><code class=\"php\">echo strtotime(&quot;2023-08-06 15:30:00&quot;), PHP_EOL;\necho strtotime(&quot;tomorrow&quot;), PHP_EOL;\necho strtotime(&quot;+1 day&quot;), PHP_EOL;<\/code><\/pre>\n<h2>2. date \u51fd\u6570<\/h2>\n<p><code>date<\/code>\u51fd\u6570\u7528\u4e8e\u5c06 Unix \u65f6\u95f4\u6233\u683c\u5f0f\u5316\u4e3a\u6240\u9700\u7684\u65e5\u671f\u548c\u65f6\u95f4\u5b57\u7b26\u4e32\u3002\u5b83\u63a5\u53d7\u4e00\u4e2a\u683c\u5f0f\u5b57\u7b26\u4e32\u548c\u4e00\u4e2a Unix \u65f6\u95f4\u6233\u4f5c\u4e3a\u53c2\u6570\uff0c\u7136\u540e\u8fd4\u56de\u4e00\u4e2a\u683c\u5f0f\u5316\u540e\u7684\u65e5\u671f\u65f6\u95f4\u5b57\u7b26\u4e32\u3002\u4ee5\u4e0b\u662f<code>date<\/code>\u51fd\u6570\u7684\u53c2\u6570\u548c\u4f5c\u7528\uff1a<\/p>\n<pre><code class=\"php\">date(string $format, ?int $timestamp = null): string<\/code><\/pre>\n<ul>\n<li>\u53c2\u6570\uff1a<code>string $format, ?int $timestamp = null<\/code><\/li>\n<li><code>$format<\/code>\uff1a\u65e5\u671f\u65f6\u95f4\u683c\u5f0f\u5b57\u7b26\u4e32\uff0c\u5176\u4e2d\u5305\u542b\u5404\u79cd\u683c\u5f0f\u5316\u9009\u9879\uff0c\u7528\u4e8e\u5b9a\u4e49\u8f93\u51fa\u7684\u65e5\u671f\u65f6\u95f4\u6837\u5f0f\u3002<\/li>\n<li><code>$timestamp<\/code>\uff1a\u53ef\u9009\u53c2\u6570\uff0c\u8868\u793a\u9700\u8981\u683c\u5f0f\u5316\u7684 Unix \u65f6\u95f4\u6233\u3002\u9ed8\u8ba4\u4e3a<code>time()<\/code>\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff0c\u5373\u5f53\u524d\u7684 Unix \u65f6\u95f4\u6233\u3002<\/li>\n<li>\u8fd4\u56de\u503c\uff1a\u6839\u636e\u6307\u5b9a\u683c\u5f0f\u8fd4\u56de\u683c\u5f0f\u5316\u540e\u7684\u65e5\u671f\u65f6\u95f4\u5b57\u7b26\u4e32\u3002<\/li>\n<\/ul>\n<p><code>date<\/code>\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u65e5\u671f\u683c\u5f0f\u5b57\u7b26\u4e32\uff0c\u5176\u4e2d\u5305\u542b\u5404\u79cd\u683c\u5f0f\u5316\u9009\u9879\uff0c\u4f8b\u5982&#8221;Y&#8221;\u4ee3\u8868\u5e74\u4efd\uff0c&#8221;m&#8221;\u4ee3\u8868\u6708\u4efd\uff0c&#8221;d&#8221;\u4ee3\u8868\u65e5\u671f\uff0c&#8221;H&#8221;\u4ee3\u8868\u5c0f\u65f6\uff0c&#8221;i&#8221;\u4ee3\u8868\u5206\u949f\uff0c&#8221;s&#8221;\u4ee3\u8868\u79d2\u7b49\u3002<\/p>\n<pre><code class=\"php\">\/\/ set the default timezone to use.\ndate_default_timezone_set('UTC');\n\n\n\/\/ Prints something like: Monday\necho date(&quot;l&quot;);\n\n\/\/ Prints something like: Monday 8th of August 2005 03:12:46 PM\necho date('l jS \/of F Y h:i:s A');\n\n\/\/ Prints: July 1, 2000 is on a Saturday\necho &quot;July 1, 2000 is on a &quot; . date(&quot;l&quot;, mktime(0, 0, 0, 7, 1, 2000));\n\n\/* use the constants in the format parameter *\/\n\/\/ prints something like: Wed, 25 Sep 2013 15:28:57 -0700\necho date(DATE_RFC2822);\n\n\/\/ prints something like: 2000-07-01T00:00:00+00:00\necho date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000));<\/code><\/pre>\n<p>\u901a\u8fc7\u7ec4\u5408\u8fd9\u4e9b\u9009\u9879\uff0c\u5c31\u53ef\u4ee5\u521b\u5efa\u51fa\u5404\u79cd\u4e0d\u540c\u7684\u65e5\u671f\u548c\u65f6\u95f4\u683c\u5f0f\u3002<\/p>\n<h2>3. DateTimeImmutable::createFromFormat \u65b9\u6cd5<\/h2>\n<p><code>DateTimeImmutable::createFromFormat<\/code>\u65b9\u6cd5\u662f\u9762\u5411\u5bf9\u8c61\u7684\u65e5\u671f\u548c\u65f6\u95f4\u5904\u7406\u65b9\u5f0f\uff0c\u6839\u636e\u6307\u5b9a\u7684\u683c\u5f0f\u5c06\u65e5\u671f\u5b57\u7b26\u4e32\u89e3\u6790\u4e3a<code>DateTimeImmutable<\/code>\u5bf9\u8c61\u3002<\/p>\n<p>\u8fd9\u5bf9\u4e8e\u5904\u7406\u4e0d\u540c\u5730\u533a\u7684\u65e5\u671f\u683c\u5f0f\u6216\u9700\u8981\u66f4\u7cbe\u786e\u89e3\u6790\u7684\u65e5\u671f\u5b57\u7b26\u4e32\u975e\u5e38\u6709\u7528\u3002<\/p>\n<pre><code class=\"php\">public static DateTimeImmutable::createFromFormat(string $format, string $datetime, ?DateTimeZone $timezone = null): DateTimeImmutable|false\n\ndate_create_immutable_from_format(string $format, string $datetime, ?DateTimeZone $timezone = null): DateTimeImmutable|false<\/code><\/pre>\n<p>\u4ee5\u4e0b\u662f<code>DateTimeImmutable::createFromFormat<\/code>\u65b9\u6cd5\u7684\u53c2\u6570\u548c\u4f5c\u7528\uff1a<\/p>\n<ul>\n<li>\u53c2\u6570\uff1a<code>string $format, string $datetime, ?DateTimeZone $timezone = null<\/code><\/li>\n<li><code>$format<\/code>\uff1a\u65e5\u671f\u65f6\u95f4\u683c\u5f0f\u5b57\u7b26\u4e32\uff0c\u7528\u4e8e\u6307\u5b9a\u8f93\u5165\u7684\u65e5\u671f\u65f6\u95f4\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\u3002<\/li>\n<li><code>$datetime<\/code>\uff1a\u9700\u8981\u89e3\u6790\u7684\u65e5\u671f\u65f6\u95f4\u5b57\u7b26\u4e32\u3002<\/li>\n<li><code>$timezone<\/code>\uff1a\u53ef\u9009\u53c2\u6570\uff0c\u7528\u4e8e\u8bbe\u7f6e\u89e3\u6790\u540e\u7684<code>DateTimeImmutable<\/code>\u5bf9\u8c61\u7684\u65f6\u533a\u3002\u5982\u679c\u4e0d\u6307\u5b9a\uff0c\u9ed8\u8ba4\u4e3a<code>null<\/code>\uff0c\u8868\u793a\u4f7f\u7528\u670d\u52a1\u5668\u7684\u65f6\u533a\u8bbe\u7f6e\u3002<\/li>\n<li>\u8fd4\u56de\u503c\uff1a\u5982\u679c\u89e3\u6790\u6210\u529f\uff0c\u5219\u8fd4\u56de\u4e00\u4e2a<code>DateTimeImmutable<\/code>\u5bf9\u8c61\uff0c\u5982\u679c\u89e3\u6790\u5931\u8d25\uff0c\u5219\u8fd4\u56de<code>false<\/code>\u3002<\/li>\n<\/ul>\n<p>\u4f7f\u7528<code>DateTimeImmutable::createFromFormat<\/code>\u65b9\u6cd5\u65f6\uff0c\u60a8\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u683c\u5f0f\u5b57\u7b26\u4e32\uff0c\u8be5\u683c\u5f0f\u5b57\u7b26\u4e32\u4e0e\u8f93\u5165\u7684\u65e5\u671f\u65f6\u95f4\u5b57\u7b26\u4e32\u76f8\u5339\u914d\u3002<\/p>\n<pre><code class=\"php\">$dateString = &quot;06\/08\/2023&quot;;\n$format = &quot;d\/m\/Y&quot;;\n$dateTime = DateTimeImmutable::createFromFormat($format, $dateString);\n\nif ($dateTime instanceof DateTimeImmutable) {\n    echo $dateTime-&gt;format(&quot;Y-m-d&quot;); \/\/ \u8f93\u51fa\uff1a2023-08-06\n}<\/code><\/pre>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u5728 PHP \u4e2d\uff0c\u65e5\u671f\u548c\u65f6\u95f4\u5904\u7406\u662f\u4e00\u4e2a\u5e38\u89c1\u4f46\u590d\u6742\u7684\u4efb\u52a1\u3002<code>strtotime<\/code>\u3001<code>date<\/code>\u548c<code>DateTimeImmutable::createFromFormat<\/code>\u8fd9\u4e09\u4e2a\u51fd\u6570\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u4f7f\u5f97\u5904\u7406\u4e0d\u540c\u683c\u5f0f\u7684\u65e5\u671f\u548c\u65f6\u95f4\u53d8\u5f97\u66f4\u52a0\u4fbf\u6377\u3002<\/p>\n<p><code>strtotime<\/code>\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a Unix \u65f6\u95f4\u6233\uff0c<code>date<\/code>\u7528\u4e8e\u5c06\u65f6\u95f4\u6233\u683c\u5f0f\u5316\u4e3a\u53ef\u8bfb\u7684\u5b57\u7b26\u4e32\uff0c\u800c<code>DateTimeImmutable::createFromFormat<\/code>\u5219\u5141\u8bb8\u66f4\u7cbe\u786e\u5730\u89e3\u6790\u65e5\u671f\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u901a\u8fc7\u719f\u7ec3\u638c\u63e1\u8fd9\u4e9b\u51fd\u6570\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u5904\u7406\u548c\u7ba1\u7406\u65e5\u671f\u65f6\u95f4\u76f8\u5173\u7684\u4efb\u52a1\uff0c\u63d0\u5347 Web \u5f00\u53d1\u6548\u7387\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000044157081\">https:\/\/segmentfault.com\/a\/1190000044157081<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Web \u5f00\u53d1\u4e2d\uff0c\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u4efb\u52a1\u3002PHP \u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u5f3a\u5927\u7684\u65e5\u671f\u548c\u65f6\u95f4\u5904\u7406\u51fd\u6570\uff0c\u4f8b\u5982strt [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10008182","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008182","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10008182"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008182\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10008182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10008182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10008182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}