簡體   English   中英

如何獲得最近3個月,數周或數年或數天的PHP?

[英]How to get last 3 months or weeks or years or days in PHP?

我有最近30天的工作代碼。

<?php
    echo date('Y-m-d').' - '.date('Y-m-d', strtotime('today - 30 days'));
?>

我還想使其更具動態性,例如:

`date('Y-m-d', strtotime('from today to 3 months'));`
`date('Y-m-d', strtotime('from today to 7 weeks'));` 
`date('Y-m-d', strtotime('from today to 2 years'));` 
echo date('Y-m-d', strtotime('- 3 months')).'<br>';
echo date('Y-m-d', strtotime('- 7 weeks')).'<br>';
echo date('Y-m-d', strtotime('- 2 years'));

today沒有必要在strtotime()函數中指定。

要使其動態使用variables ,例如:

$amount = 5;
$field = 'months'; // or 'days' or 'years'...
echo date('Y-m-d', strtotime("- $amount $field")).'<br>';

您當前的代碼看起來還不錯,但是您可以省略“今天”部分,然后執行以下操作:

date('Y-m-d', strtotime('-3 months'));
date('Y-m-d', strtotime('-7 weeks'));
date('Y-m-d', strtotime('-2 years'));

請檢查此答案以獲取從今天起的最后日期:

<?php
  $today =  date('Y-m-d');
  echo $lastthereemonth = date('Y-m-d',strtotime('-3 month',strtotime($today))); echo "<br/>";
  echo $lastthereeweeks = date('Y-m-d',strtotime('-3 weeks',strtotime($today)));echo "<br/>";
  echo $lastthereedays = date('Y-m-d',strtotime('-3 day',strtotime($today)));
?>

暫無
暫無

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

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