2015-04-12 85 views
0

我正試圖從數據庫中找到應該在30天后的日期。一個月的差距計算

我想從數據庫中檢索出格式爲DateTime的日期。將其更改爲時間戳strtotime()。將其提供給date()並檢索mi,然後進行本地計算以確定它是否不到三十天。

我想知道是否有任何簡單易行的程序來做到這一點。

+0

DB是什麼? MySQL的? –

+0

@YasenZhelev是的。 –

+0

PHP中的所有內容都有很好的記錄。只要看一下'strtotime()'文檔中的例子,你會在幾秒鐘內找到答案:http://php.net/manual/en/function.strtotime.php#refsect1-function.strtotime-examples – Marc

回答

0
date('Y-m-d H:i:s', strtotime(' +30 day')); 

這也將工作

date('Y-m-d H:i:s', strtotime(' +1 month')); 

或使用任何你需要的格式,但這種方法計算一個日期加30天

PHP strtotime

相關答案:

  1. PHP: get next 13 dates from date?
  2. get next and previous day with php
+0

@azamkhan,可能你將不得不隱瞞日期轉換爲日期時間格式以便在數據庫中使用(這取決於您在數據庫中使用的日期格式),本示例使用Ymd格式,可以使用此格式,甚至可以使用http://php.net/manual/en/function.date- parse.php –

0

試試這個:

$date_formatted = date('Y-m-d H:i:s', strtotime($val->time)); 

要檢索mi或兩者兼而有之,試試這個:

$v = date('Y-m-d H:i:s'); //test it with now. 
$date_formatted = date('m i', strtotime($v)); 

內容查找日期從數據庫中恰好30天從現在開始你可以試試這個:

$date_formatted = date('Y-m-d H:i:s', strtotime(' +30 day')); 
0

如果你想直接在SQL中做到這一點,你可以做到這一點。

WHERE DATE_ADD(NOW(), INTERVAL 1 MONTH) = your_date_column_here 

這樣,您可以直接獲取僅提前1個月查詢的日期。

或者,如果你想獲得那就是提前一個月從今天用PHP使用這種

strtotime('+ 1 month'); 

使用「+1 mohth」而不是「30天」日期的UNIX時間戳總會給你提前一個月,而不僅僅是提前30天。