2017-04-13 103 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手册等格式在查找那里有日期时间吨