首页 > 解决方案 > 在 php 或 javascript 中列出带有日期名称的每个日期

问题描述

有没有办法在 php 或 javascript 中列出每个日期和日期名称?例子

  1. 1月1日星期三
  2. 1月2日星期四
  3. 1月3日星期五

等等等等,如果有人可以传播一些关于是否以及如何做到这一点的信息,我将不胜感激。谢谢你。

标签: phpdateecho

解决方案


您可以使用DatePeriod.

$start = new DateTime('1/1'); // first day of the current year
$interval = new DateInterval('P1D'); // iterate 1 day at a time
$end = new DateTime('1/1 next year'); // stop when we get to first day of next year
// All of these periods are equivalent.
$period = new DatePeriod($start, $interval, $end);

// By iterating over the DatePeriod object, all of the
// recurring dates within that period are printed.
foreach ($period as $date) {
    echo $date->format('Y-m-d')."\n";
}

这将为您提供当年的每一天。


推荐阅读