2017-04-13 68 views
1

我在php文件中有下面的請求。我試圖讓這一格式的日期:個月currentMonth-currentYear選擇日期mysql請求

$sql = "SELECT DAY(ADDDATE(`dateDebutC`, `dureeC`)) AS MONTHS FROM normalW WHERE id = '$id'"; 
+0

你爲什麼不使用日期時間來格式化它,一旦你從Mysql返回它? – clearshot66

+0

你可以給我代碼 –

+0

當你選擇MONTHS,當它通過在PHP中的結果是什麼它回聲在? – clearshot66

回答

0

如果你想在從sql查詢返回值後格式化日期,然後您可以使用PHP date_format()函數。假設你存儲幾個月值的變量,我們稱之爲$date

echo date_format($date,'Y-m-d'); // Or whatever format you want to display 

爲當月和當年添加到字符串,可以使用date()功能:

$current_month = date(m); // prints current month in numeric format (01-12) 
$current_year = date(Y); // prints current year as a 4-digit representation 
$new_date = $date . '-' . $current_month . '-' . $current_year; 

https://www.w3schools.com/php/func_date_date.asp獲取所有參數的列表。作爲一個友好的提醒,請確保您使用的是mysqli而不是mysql,這已棄用。

+0

非常好,您可以通過點擊投票箭頭下方的複選標記來接受此答案。 :) –

0

要獲取當前月份和年份,您可以使用GETDATE功能SQL

$sql = "SELECT MONTH(ADDDATE(`dateDebutC`, `dureeC`))AS MONTHS,MONTH(GETDATE()) as curmonth,YEAR(GETDATE()) as curyear FROM normalW where id = '$id'"; 
+0

以及如何使用這些信息創建日期 –

+0

您可以在PHP中執行此操作嗎? – lalithkumar

0

但是你不管這是以通過作爲結果,例如,將其綁定:

$date = '2014-04-15 10:10:11'; 
$buildDate   = new DateTime(strtotime($date)); 
$dowMonthDayYear = $buildDate->format('l F j, Y'); 

echo dowMonthDayYear // turns as April 15th, 2014 

可以PHP手冊等格式在查找那裏有日期時間噸