Q
日期減1年?
61
A
回答
96
69
使用的strtotime()函數:
$time = strtotime("-1 year", time());
$date = date("Y-m-d", $time);
8
// set your date here
$mydate = "2009-01-01";
/* strtotime accepts two parameters.
The first parameter tells what it should compute.
The second parameter defines what source date it should use. */
$lastyear = strtotime("-1 year", strtotime($mydate));
// format and display the computed date
echo date("Y-m-d", $lastyear);
31
使用DateTime對象......今天
$time = new DateTime('2099-01-01');
$newtime = $time->modify('-1 year')->format('Y-m-d');
還是現在使用
$time = new DateTime('now');
$newtime = $time->modify('-1 year')->format('Y-m-d');
12
我用它和行之有效
date('Y-m-d', strtotime('-1 year'));
這個工作完美的最簡單的方法..希望這會幫助別人.. :)
0
你可以使用followi ng函數從日期中減去1或任何年份。
function yearstodate($years) {
$now = date("Y-m-d");
$now = explode('-', $now);
$year = $now[0];
$month = $now[1];
$day = $now[2];
$converted_year = $year - $years;
echo $now = $converted_year."-".$month."-".$day;
}
$number_to_subtract = "1";
echo yearstodate($number_to_subtract);
而且看着上面的例子中,您還可以使用以下
$user_age_min = "-"."1";
echo date('Y-m-d', strtotime($user_age_min.'year'));
相關問題
- 1. 從日期時間減去1年
- 2. 顯示明年日期(加1年)
- 3. 如何在MSSQL中使用日期範圍減去1年?
- 4. 的QlikView - 減1年
- 5. 解析日期「2016年1月1日星期五」
- 6. 年份日期:日期('z')+1; (1..366)(001..366)格式
- 7. 減去當前日期的年份
- 8. SQL變量日期減一年
- 9. Sharepoint - 結束日期減去開始日期=年?
- 10. 計算人年減去開始日期從結束日期
- 11. 散景:當選擇的開始日期是1月1日時,DateRangeSlider將年份減1
- 12. 減少午夜以來的毫秒數,1970年1月1日
- 13. T-SQL轉換2010年1月28日至2010年1月28日星期五
- 14. VB.net從DateTime.Now.ToString減去1年
- 15. 如何保持閏年,減少1年
- 16. 如何獲得日期 - 1年vba
- 17. PHP的:日期+1顯示1970年
- 18. PHP今天的日期+ 1年
- 19. NSDateFormatter返回2001年1月1日作爲日期
- 20. 日期格式在2012年1月1日SQL
- 21. 1970年1月1日之前的日期
- 22. 爲什麼從1970年1月1日起計算日期?
- 23. 1970年1月1日之前轉換日期在R
- 24. 在moment.js格式化日期給出2016年1月1日
- 25. SQL申報日期時間 - 1001年1月1日
- 26. iPhone NSDateComponent錯日期2011年1月1日?
- 27. Oracle日期 - 獲得給定年份的1月1日
- 28. 遞減日期由1按下按鈕
- 29. 在Rails中,我如何顯示日期,如2009年12月1日星期一,2009年12月1日
- 30. 比較的一年的結束日期和今年的開始日期+ 1
不要忘記傾向於你的意思是「一年」什麼語義問題w.r.t.閏年。從2008-02-28減去365天會給你2007-02-28,而從2008-02-29減去365天會給你2007-03-31。 – HostileFork 2010-01-02 02:16:23
我想這很大程度上取決於「減去一年」的含義。你可能意思是同一個月和一天,但是在一年前或者在敵對指出的365天后減少的那一天和那一天。 – 2010-01-02 02:33:05