I'm still getting used to using PHP and MYSQL so please bear with me.
I have a database table called 'employee_datetable' that with a field called "datetime" contains 365 records of actual dates for the next year (August 13, 2014 to August 13, 2015).
I want to write a MYSQL query that retrieves 7 dates and displays them in a row while in readable format such as "Sept 24, 2014".
Right now the dates are stored as date types and displayed as "2014-09-24" etc.
Any help would be greatly appreciated.
PHP Code:
$dates_sql = "SELECT UNIX_TIMESTAMP(datetime) FROM employee_datetable LIMIT 7";
$PhpDate = strtotime($dates_sql);
$FormattedPhpDate = date('M d, Y', $PhpDate );
echo "Selected Dates from the query: " . $FormattedPhpDate;
解决方案
You need to use something like mysqli or pdo to execute your query and extract the data. Here's the general idea, using mysqli:
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
$dates_sql = "SELECT UNIX_TIMESTAMP(datetime) AS tstamp FROM employee_datetable LIMIT 7";
$result = $mysqli->query($dates_sql);
while($row = $result->fetch_array()) {
$FormattedPhpDate = date('M d, Y', $row['tstamp']);
echo "Date: " . $FormattedPhpDate . "
";
}
Note: Since you are using UNIX_TIMESTAMP in your query there is no need to convert the date to a timestamp using strtotime()
For more info see documentation for mysqli, specifically mysqli_query and mysqli_fetch_array