簡體   English   中英

如何使用日期在 PHP 中獲取星期幾?

[英]How to get the name of the day in PHP using date?

是否可以在轉換為date的數字行中獲取日期名稱?

我有代碼:

$date1 = date('Y-m-01');
$Month = date('m', strtotime($date1));
$Year = date('Y', strtotime($date1));
$Maximum_Date = date('t', strtotime($date1));

for($Date = 1; $Date <= $Maximum_Date; $Date++){
  $DataDate = $Date . ' ' . $Month . ' ' . $Year . '<BR>';
  echo $DataDate;
}

結果:

1 04 2019
2 04 2019
3 04 2019
etc..

我想要的是將其更改為顯示該日期的日期名稱

例如四月份的日期:

Monday, 1 04 2019
Tuesday, 2 04 2019
Wednesday, 3 04 2019
etc..

[更新] 2019 年 4 月 15 日參考評論,我在這里查看文檔並使用mktime();

所以我更新代碼:

$date1 = date('Y-m-01');
$Month = date('m', strtotime($date1));
$Year = date('Y', strtotime($date1));
$Maximum_Date = date('t', strtotime($date1));

for($Date = 1; $Date <= $Maximum_Date; $Date++){
  echo date("l, d m Y", mktime(0, 0, 0, $Month, $Date, $Year)) . '<br>';
}

並得到結果:

Monday, 1 04 2019
Tuesday, 2 04 2019
Wednesday, 3 04 2019
etc..

您可以大大簡化這一點,而無需在datestrtotime之間來回轉換:

$year = date('Y');
$month = date('n');
$lastDay = date('t');

foreach (range(1, $lastDay) as $day) {
    echo date('D, j m Y', mktime(0, 0, 0, $month, $day, $year)), '<br>';
}

請參閱http://php.net/mktime

省略隱式默認值並將其壓縮一點,實際上您可以將其歸結為:

foreach (range(1, date('t')) as $day) {
    echo date('D, j m Y', mktime(0, 0, 0, date('n'), $day)), '<br>';
}
Mon, 1 04 2019
Tue, 2 04 2019
Wed, 3 04 2019
...
Tue, 30 04 2019

請注意,如果您在一個月滾動到下一個月的第二秒執行它,並且date('n')date('t')函數恰好被稱為“在不同的月份”。 為完全避免這種可能性,請將此操作設為原子操作:

list($year, $month, $lastDay) = explode(' ', date('Y n t'));

foreach (range(1, $lastDay) as $day) {
    echo date('D, j m Y', mktime(0, 0, 0, $month, $day, $year)), '<br>';
}

下面的代碼將打印出您需要的內容..

$date1 = date('Y-m-01');
$Maximum_Date = date('t', strtotime($date1));

for($Date = 1; $Date <= $Maximum_Date; $Date++)
{
    $thatDay = date('Y-m-'.$Date);
    $day = date('l, j', strtotime($thatDay));
    $Month = date('m', strtotime($thatDay));
    $Year = date('Y', strtotime($thatDay));

    echo $day . ' ' . $Month . ' ' . $Year . '<BR>';
}

輸出:

Monday, 1 04 2019
Tuesday, 2 04 2019
Wednesday, 3 04 2019
Thursday, 4 04 2019
Friday, 5 04 2019
...
...

如果你改變這一行

$day = date('l, j', strtotime($thatDay));

$day = date('l, jS', strtotime($thatDay));

輸出將是: 2019 年 1 月 1 日星期一

采用:

$date1 = date('Y-m-01');
$Month = date('m', strtotime($date1));
$Year = date('Y', strtotime($date1));
$Maximum_Date = date('t', strtotime($date1));

for($Date = 1; $Date <= $Maximum_Date; $Date++){
  $DataDate = date("D-m-Y",strtotime($Year."-".$Month."-".$Date));
  echo $DataDate. '<br>';;
}

你應該像下面這樣嘗試。

$date1 = date('Y-m-01');
$Month = date('m', strtotime($date1));
$Year = date('Y', strtotime($date1));
$Maximum_Date = date('t', strtotime($date1));

$StartDate=$date1;
$EndDate=($Year."-".$Month."-".$Maximum_Date);
$EndDate=date('Y-m-d',strtotime($EndDate));

while(strtotime($EndDate)>=strtotime($StartDate))
{
    echo date('l, d-m-Y',strtotime($StartDate))."<br/>";
    $StartDate=date('Y-m-d', strtotime($StartDate . ' +1 day'));
}

執行后你會得到如下結果。

Monday, 01-04-2019
Tuesday, 02-04-2019
Wednesday, 03-04-2019
...................
...................
...................
...................
Saturday, 27-04-2019
Sunday, 28-04-2019
Monday, 29-04-2019
Tuesday, 30-04-2019

將以下行添加到循環中:

    echo (new DateTime($Date. '-'. $Month .'-'. $Year))->format('l');

它使用您的數據創建一個新的 DateTime 對象,並以您請求的格式回顯日期。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM