可以通过 PHP 中的 DateTime 类及其方法获取日历数据:获取当前日期和时间:new DateTime()创建特定日期的对象:new DateTime('特定日期字符串')获取日历组件:使用 format() 方法提取年份、月份、日期等获取当前月:new DateTime() 的 format('m')获取当前年:new DateTime() 的 format('Y')循环当前月的天数:for 循环 i 从 1 遍历到 format('t')获取某一天的星期几:format('l')获取

如何获取 PHP 日历
在 PHP 中,您可以使用 DateTime 类及其相关方法来获取和操作日历数据。
获取当前日期和时间
<code class="php">$now = new DateTime();
echo $now->format('Y-m-d H:i:s'); // 输出:2023-03-08 13:26:49</code>创建特定日期的对象
立即学习“PHP免费学习笔记(深入)”;
<code class="php">$specificDate = new DateTime('2023-04-01');
echo $specificDate->format('l, F jS, Y'); // 输出:Saturday, April 1st, 2023</code>获取日历组件
<code class="php">echo $specificDate->format('Y'); // 2023
echo $specificDate->format('m'); // 04
echo $specificDate->format('d'); // 01</code>获取当前月
<code class="php">$currentMonth = (int) $now->format('m');</code>获取当前年
<code class="php">$currentYear = (int) $now->format('Y');</code>循环当前月的天数
<code class="php">for ($i = 1; $i <= $specificDate->format('t'); $i++) {
echo $i . "<br>"; // 1-30
}</code>获取某一天的星期几
<code class="php">echo $specificDate->format('l'); // Saturday</code>获取某一天的第几周
<code class="php">echo $specificDate->format('W'); // 14</code>以上就是php日历怎么获取的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号