How to echo date parts separately
I have found place online where they answer different things, but I still don't get how I can apply it to my code.
Basically I want the month in one echo-line, and the day-number in another echo.
Is there away to do that in my code?
<?php
// Connects to your Database
$data = mysql_query("SELECT id, title, description, location, date FROM Calendar")
or die(mysql_error());
while($row = mysql_fetch_array( $data ))
{
e开发者_Go百科cho "<article>";
echo "<date>";
echo "<h3>" .$row['date(month)'] . "</h3>";
echo "<h4>" .$row['date(day)'] . "</h4>";
echo "</date>";
echo "</article>";
}
?>
You should be able to just pass the date straight through strtotime:
$month = date('m', strtotime($row['date']));
$day = date('d', strtotime($row['date']));
But this will depend on the format the date is stored in the database. This should work for a TIMESTAMP
type column or a UNIX timestamp, or (for the most part) a string representation of a date.
Here's a way to get them ahead of time in your MySQL query, as an alternative to parsing them out later in PHP.
$data = mysql_query("SELECT id, title, description, location, DATE_FORMAT(date, '%m') AS `month`, DATE_FORMAT(date, '%d') AS `day` FROM Calendar")
or die(mysql_error());
Then to access them:
while($row = mysql_fetch_array( $data ))
{
echo $row['month'];
echo $row['day'];
}
Alternatively to get the month's name instead of number, use DATE_FORMAT(date, '%M') as month
MySQL DATE_FORMAT()
documentation
You first get the unix time of the date and then format two different strings.
If you want to echo date as exactly the name of the month, you can use 'F'. Like this:
$month = date('F', strtotime($row['dateRepaired']));
精彩评论