{"id":49310,"date":"2024-12-02T10:26:49","date_gmt":"2024-12-02T02:26:49","guid":{"rendered":"https:\/\/fwq.ai\/blog\/49310\/"},"modified":"2024-12-02T10:26:49","modified_gmt":"2024-12-02T02:26:49","slug":"php%ef%bc%9a%e9%80%83%e7%a6%bb%e6%97%a5%e6%9c%9f%e5%9c%b0%e7%8b%b1%e7%9a%84%e5%ae%9e%e7%94%a8%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/49310\/","title":{"rendered":"PHP\uff1a\u9003\u79bb\u65e5\u671f\u5730\u72f1\u7684\u5b9e\u7528\u65b9\u6cd5"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP\uff1a\u9003\u79bb\u65e5\u671f\u5730\u72f1\u7684\u5b9e\u7528\u65b9\u6cd5<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u6211\u4eec\u8981\u52aa\u529b\u5b66\u4e60\u5566\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP\uff1a\u9003\u79bb\u65e5\u671f\u5730\u72f1\u7684\u5b9e\u7528\u65b9\u6cd5\u300b<\/span>\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4e0b\u6587\u4e2d\u7684\u5185\u5bb9\u6211\u4eec\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5728\u9605\u8bfb\u672c\u6587\u8fc7\u7a0b\u4e2d\u6709\u9047\u5230\u4e0d\u6e05\u695a\u7684\u5730\u65b9\uff0c\u6b22\u8fce\u7559\u8a00\u5440\uff01\u6211\u4eec\u4e00\u8d77\u8ba8\u8bba\uff0c\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p>\u4f01\u4e1a\u7684\u5173\u952e\u529f\u80fd\u901a\u5e38\u4f9d\u8d56\u4e8e\u65e5\u671f\uff0c\u4f8b\u5982\u8ba2\u9605\u3001\u5b9a\u671f\u4ed8\u6b3e\u6216\u9884\u8ba2\u3002 <\/p>\n<p>\u4f5c\u4e3a\u4e00\u540d php \u7a0b\u5e8f\u5458\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u64cd\u4f5c\u65e5\u671f\/\u65f6\u95f4\u3002<\/p>\n<h2> \u5f00\u59cb\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff1f\u771f\u7684\u5417\uff1f <\/h2>\n<p>\u867d\u7136\u575a\u6301\u4f7f\u7528\u672c\u673a api \u6765\u638c\u63e1\u6280\u80fd\u5e76\u4e86\u89e3\u8bed\u8a00\u7684\u5de5\u4f5c\u539f\u7406\u901a\u5e38\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u505a\u6cd5\uff0c\u4f46\u8ba9\u6211\u4eec\u52a1\u5b9e\u4e00\u4e9b\u3002 <\/p>\n<p>carbon \u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u5e93\uff0c\u5b83\u6269\u5c55\u4e86 datetimeinterface\u3001jsonserialized \u548c\u5176\u4ed6\u539f\u751f php \u5de5\u5177\u3002 <\/p>\n<p>\u5b83\u4e3a\u64cd\u4f5c\u73b0\u6709\u548c\u65b0\u7684\u65e5\u671f\u5bf9\u8c61\u63d0\u4f9b\u4e86\u6709\u7528\u7684\u5e2e\u52a9\u5668\uff1a<\/p>\n<pre>\/\/ tomorrow\ncarbonimmutable::now()-&gt;add(1, 'day');\n\n\/\/ copy instance from another api\n$datetime = new \\datetime('first day of january 2025');\n$carbon = carbon::instance($datetime);\n\n\/\/ parsing values\n$date = carbon::parse('2050-01-01 12:34:00', 'utc');\n<\/pre>\n<h2> \u56de\u5230\u57fa\u7840 <\/h2>\n<p>carbon \u6269\u5c55\u4e86\u539f\u751f api\uff0c\u4f46 datetime \u5bf9\u8c61\u5df2\u7ecf\u63d0\u4f9b\u4e86\u8ba1\u7b97\u65e5\u671f\u548c\u65f6\u95f4\u7684\u5de5\u5177\uff1a<\/p>\n<pre>$date = new datetime('2024-07-23');\n$date-&gt;add(new dateinterval('p10d'));\necho $date-&gt;format('y-m-d');\n\n$date2 = new datetime('2024-08-03');\n$interval = $date-&gt;diff($date2);\necho $interval-&gt;format('%r%a days');\n<\/pre>\n<p>\u60a8\u53ef\u4ee5\u4f20\u9012\u5b57\u7b26\u4e32\u6765\u83b7\u53d6\u7279\u5b9a\u8f93\u51fa\u6216\u8ba1\u7b97\u95f4\u9694\u548c\u5468\u671f\u3002<\/p>\n<h2> \u90a3\u4e48\uff0c\u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528\u78b3\u7b49\u9644\u52a0\u5c42\u5462\uff1f <\/h2>\n<h3> \u66f4\u597d\u7684\u53ef\u8bfb\u6027 <\/h3>\n<pre>$yesterday = carbon::yesterday();\n$today = carbon::today();\n$now = carbon::now();\n$tomorrow = carbon::tomorrow();\n$futuredate = carbon::today()-&gt;adddays(21)-&gt;format('y-m-d');\n\nif ($now-&gt;isendofday()) {\n    \/\/ some code\n}\n<\/pre>\n<p>\u8be5\u5e93\u5bf9\u4e8e\u65e5\u671f\u6bd4\u8f83\u6765\u8bf4\u65b9\u4fbf\u4e14\u5f3a\u5927\uff0c\u4f46\u5f88\u5bb9\u6613\u51fa\u73b0\u5404\u79cd\u9519\u8bef\u3002<\/p>\n<h3> \u8f7b\u677e\u672c\u5730\u5316 <\/h3>\n<p>carbon \u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u8bed\u8a00\u73af\u5883\uff1a<\/p>\n<pre>$now = carbon::now()-&gt;locale('fr_fr');\necho $now-&gt;monthname;\n<\/pre>\n<h3> \u5904\u7406\u6ea2\u51fa <\/h3>\n<p>\u9ed8\u8ba4\u7684 php datetime \u884c\u4e3a\u5728\u6dfb\u52a0\u6216\u51cf\u53bb\u6708\u4efd\u65f6\u53ef\u80fd\u4f1a\u4ea7\u751f\u8bef\u5bfc\u3002\u5b83\u53ef\u4ee5\u6ea2\u51fa\uff01 <\/p>\n<p>\u867d\u7136\u60a8\u6709\u65f6\u53ef\u80fd\u9700\u8981\u8fd9\u79cd\u884c\u4e3a\uff0c\u4f46 carbon \u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u63a7\u5236\uff1a<\/p>\n<pre>carbon::usemonthsoverflow(false);\ncarbon::resetmonthsoverflow();\n<\/pre>\n<p>\u60a8\u8fd8\u53ef\u4ee5\u5728\u64cd\u4f5c\u5e74\u6216\u6708\u65f6\u5728\u6bcf\u4e2a\u65b9\u6cd5\u4e2d\u914d\u7f6e\u6ea2\u51fa\uff08\u4f8b\u5982\uff0csubmonthsnooverflow\u3001addmonthswithoverflow\uff09\u3002<\/p>\n<h3> \u5355\u5143\u6d4b\u8bd5\u65e5\u671f <\/h3>\n<p>\u7531\u4e8e\u53ef\u53d8\u6027\uff0c\u65e5\u671f\u5728\u5355\u5143\u6d4b\u8bd5\u4e2d\u53ef\u80fd\u4f1a\u51fa\u73b0\u95ee\u9898\u3002 <\/p>\n<p>carbon \u63d0\u4f9b\u4e86\u5148\u8fdb\u7684\u6a21\u62df\u529f\u80fd\uff0c\u786e\u4fdd\u6d4b\u8bd5\u4fdd\u6301\u4e00\u81f4\u548c\u53ef\u9760\u3002 <\/p>\n<p>\u8fd9\u53ef\u4ee5\u9632\u6b62\u968f\u673a\u9519\u8bef\u7834\u574f ci\/cd \u7ba1\u9053\u3002<\/p>\n<h2> \u5982\u4f55\u907f\u514d\u65e5\u671f\/\u65f6\u95f4\u7684\u5e38\u89c1\u9677\u9631 <\/h2>\n<p>\u65e0\u8bba\u60a8\u662f\u5426\u4f7f\u7528 carbon\uff0c\u90fd\u6709\u4e00\u4e9b\u5b9e\u7528\u7684\u65b9\u6cd5\u53ef\u4ee5\u51cf\u5c11\u6f5c\u5728\u7684\u9519\u8bef\u548c\u4e0d\u4e00\u81f4\uff1a<\/p>\n<h3> \u4e0d\u8981\u6d88\u9664\u9519\u8bef <\/h3>\n<p>php \u7684\u6700\u65b0\u7248\u672c\u5f15\u5165\u4e86\u66f4\u597d\u7684\u65e5\u671f\/\u65f6\u95f4\u5f02\u5e38\uff0c\u4f7f\u60a8\u80fd\u591f\u66f4\u4e00\u81f4\u5730\u6355\u83b7\u610f\u5916\u8f93\u5165\u3002 <\/p>\n<p>\u4f46\u662f\uff0c\u5b9e\u73b0\u6709\u65f6\u53ef\u4ee5\u6d88\u9664\u4e25\u91cd\u9519\u8bef\uff1a<\/p>\n<pre>$dateinput = '2dsds';\n$timestamp = strtotime($dateinput);\necho date('y-m-d', $timestamp); \/\/ defaults back to 1970-01-01\n<\/pre>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u5e76\u4e0d\u662f\u6539\u8fdb\uff0c\u5373\u4f7f\u5b83\u4f7f\u7528 datetime \u5bf9\u8c61\uff1a<\/p>\n<pre>$dateinput = '2024-02-30'; \/\/ invalid date (february has 28 days, 29 at most)\n$date = new datetime($dateinput);\necho $date-&gt;format('l \\t\\h\\e js f'); \/\/ outputs \"friday the 1st march\"\n<\/pre>\n<p>\u6240\u4ee5\uff0c<strong>\u59cb\u7ec8<\/strong>\u9a8c\u8bc1\u65e5\u671f\/\u65f6\u95f4\uff1a<\/p>\n<pre>$dateinput = '2dsds';\n$timestamp = strtotime($dateinput);\n\nif (false === $timestamp) {\n    throw new invalidargumentexception();\n}\n\necho date('y-m-d', $timestamp);\n\n<\/pre>\n<p>\u5e76\u4e14\uff0c\u6df1\u5165\u68c0\u67e5\u8f93\u5165\uff1a<\/p>\n<pre>$format = 'Y-m-d';\n$dateInput = '2024-02-30';\n$dateInfo = date_parse($dateInput);\nif (!checkdate($dateInfo['day'], $dateInfo['month'], $dateInfo['year'])) {\n    throw new InvalidArgumentException();\n}\n\n$date = DateTimeImmutable::createFromFormat($format, $dateInput);\necho $date-&gt;format('l \\t\\h\\e jS F');\n<\/pre>\n<h3> \u9632\u6b62\u610f\u5916\u7a81\u53d8 <\/h3>\n<p>datetimeimmutable \u6216 carbonimmutable \u521b\u5efa\u540e\u65e0\u6cd5\u66f4\u6539\u3002\u5927\u591a\u6570\u65f6\u5019\uff0c\u60a8\u4e0d\u9700\u8981\u6539\u53d8\u521d\u59cb\u65e5\u671f\u3002 <\/p>\n<p>\u5982\u679c\u4ee3\u7801\u7684\u5176\u4ed6\u90e8\u5206\u4f9d\u8d56\u4e8e\u8be5\u5b9e\u4f8b\uff0c\u5219\u53ef\u80fd\u4f1a\u5f15\u5165\u4e25\u91cd\u7684\u9519\u8bef\u3002 <\/p>\n<p>\u4e0d\u53ef\u53d8\u683c\u5f0f\u53ef\u4ee5\u9632\u6b62\u6b64\u7c7b\u526f\u4f5c\u7528\uff0c\u5e76\u4e14\u901a\u5e38\u66f4\u6709\u5229\u4e8e\u53ef\u8bfb\u6027\u548c\u6d4b\u8bd5\u3002<\/p>\n<h3> \u4e0d\u8981\u5ffd\u89c6\u65f6\u533a <\/h3>\n<p>\u5ffd\u89c6\u65f6\u533a\u662f\u6709\u98ce\u9669\u7684\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241128\/173277491467480c023fb38.jpg\" class=\"aligncenter\" title=\"PHP\uff1a\u9003\u79bb\u65e5\u671f\u5730\u72f1\u7684\u5b9e\u7528\u65b9\u6cd5\u63d2\u56fe\" alt=\"PHP\uff1a\u9003\u79bb\u65e5\u671f\u5730\u72f1\u7684\u5b9e\u7528\u65b9\u6cd5\u63d2\u56fe\" \/> <\/p>\n<h2>php\uff1a\u4e86\u89e3\u65f6\u533a<\/h2>\n<h3>spo0q \u30fb 8 \u6708 4 \u65e5<\/h3>\n<p> #php #\u521d\u5b66\u8005 \uff03\u7f16\u7a0b <\/p>\n<p>\u5982\u679c\u60a8\u672a\u5728\u5e94\u7528\u4e2d\u6307\u5b9a\u9ed8\u8ba4\u65f6\u533a\uff0c\u5219\u670d\u52a1\u5668\u5c06\u786e\u5b9a\u5b83\u3002<\/p>\n<p>\u4f46\u662f\uff0c\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u53ef\u80fd\u90e8\u7f72\u5728\u4e0d\u540c\u65f6\u533a\u7684\u670d\u52a1\u5668\u4e0a\u3002 <\/p>\n<p>\u60a8\u53ef\u80fd\u8fd8\u4f1a\u9047\u5230\u590f\u4ee4\u65f6\u6216\u5b58\u50a8\u683c\u5f0f\uff08\u4f8b\u5982\uff0cutc \u4e0e\u5f53\u5730\u65f6\u95f4\uff09\u7b49\u590d\u6742\u95ee\u9898\uff0c\u800c\u5bf9\u4e8e\u9f13\u52b1\u826f\u597d\u5b9e\u8df5\u7684\u5e93\u6765\u8bf4\uff0c\u8fd9\u53ef\u80fd\u4f1a\u51cf\u8f7b\u4e00\u4e9b\u75db\u82e6\u3002<\/p>\n<h2> \u5305\u8d77\u6765 <\/h2>\n<p>\u539f\u751f api \u5141\u8bb8\u5bf9\u65e5\u671f\u548c\u65f6\u95f4\u8fdb\u884c\u9ad8\u7ea7\u64cd\u4f5c\uff0c\u800c carbon \u5219\u5bf9\u5176\u8fdb\u884c\u4e86\u7cbe\u7f8e\u7684\u6269\u5c55\u3002 <\/p>\n<p>php \u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u5229\u7528\u8fd9\u4e2a\u9644\u52a0\u5c42\u6765\u7b80\u5316\u8ba1\u7b97\u5e76\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002 <\/p>\n<p>\u65e0\u8bba\u60a8\u662f\u5426\u4f7f\u7528\u5b83\uff0c\u8bf7\u786e\u4fdd\u60a8\u4e86\u89e3\u672c\u673a php datetime \u5bf9\u8c61\u7684\u9ed8\u8ba4\u884c\u4e3a\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\u7c73\u4e91\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u3002<\/p>\n<p> \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb \u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP\uff1a\u9003\u79bb\u65e5\u671f\u5730\u72f1\u7684\u5b9e\u7528\u65b9\u6cd5 \u6536\u85cf \u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u6211\u4eec\u8981\u52aa\u529b\u5b66\u4e60\u5566\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u300aPHP\uff1a\u9003\u79bb\u65e5\u671f\u5730\u72f1\u7684\u5b9e\u7528\u65b9\u6cd5\u300b\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4e0b\u6587\u4e2d\u7684\u5185\u5bb9\u6211\u4eec\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5728\u9605\u8bfb\u672c\u6587\u8fc7\u7a0b\u4e2d\u6709\u9047\u5230\u4e0d\u6e05\u695a\u7684\u5730\u65b9\uff0c\u6b22\u8fce\u7559\u8a00\u5440\uff01\u6211\u4eec\u4e00\u8d77\u8ba8\u8bba\uff0c\u4e00\u8d77\u5b66\u4e60\uff01 \u4f01\u4e1a\u7684\u5173\u952e\u529f\u80fd\u901a\u5e38\u4f9d\u8d56\u4e8e\u65e5\u671f\uff0c\u4f8b\u5982\u8ba2\u9605\u3001\u5b9a\u671f\u4ed8\u6b3e\u6216\u9884\u8ba2\u3002 \u4f5c\u4e3a\u4e00\u540d php \u7a0b\u5e8f\u5458\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u64cd\u4f5c\u65e5\u671f\/\u65f6\u95f4\u3002 \u5f00\u59cb\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff1f\u771f\u7684\u5417\uff1f \u867d\u7136\u575a\u6301\u4f7f\u7528\u672c\u673a api \u6765\u638c\u63e1\u6280\u80fd\u5e76\u4e86\u89e3\u8bed\u8a00\u7684\u5de5\u4f5c\u539f\u7406\u901a\u5e38\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u505a\u6cd5\uff0c\u4f46\u8ba9\u6211\u4eec\u52a1\u5b9e\u4e00\u4e9b\u3002 carbon \u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u5e93\uff0c\u5b83\u6269\u5c55\u4e86 datetimeinterface\u3001jsonserialized \u548c\u5176\u4ed6\u539f\u751f php \u5de5\u5177\u3002 \u5b83\u4e3a\u64cd\u4f5c\u73b0\u6709\u548c\u65b0\u7684\u65e5\u671f\u5bf9\u8c61\u63d0\u4f9b\u4e86\u6709\u7528\u7684\u5e2e\u52a9\u5668\uff1a \/\/ tomorrow carbonimmutable::now()-&gt;add(1, &#8216;day&#8217;); \/\/ copy instance from another api $datetime = new \\datetime(&#8216;first day of january 2025&#8217;); $carbon = carbon::instance($datetime); \/\/ parsing values $date = carbon::parse(&#8216;2050-01-01 12:34:00&#8217;, &#8216;utc&#8217;); \u56de\u5230\u57fa\u7840 carbon \u6269\u5c55\u4e86\u539f\u751f api\uff0c\u4f46 datetime \u5bf9\u8c61\u5df2\u7ecf\u63d0\u4f9b\u4e86\u8ba1\u7b97\u65e5\u671f\u548c\u65f6\u95f4\u7684\u5de5\u5177\uff1a $date = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-49310","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49310","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=49310"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49310\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=49310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=49310"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=49310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}