PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
<?php
require_once("connect.php"); //Voor de connectie met de database.
$select = "SELECT * FROM agenda ORDER BY datum DESC LIMIT 2";
$query = mysql_query($select)or die(mysql_error());
while($list = mysql_fetch_object($query)){
echo "<b>Datum: </b>$list->datum<br/>";
echo "<b>Locatie: </b>$list->locatie<br/>";
echo "<b>Plaats: </b>$list->plaats<br/>";
echo "<img src=\"images/line-leftbar.jpg\"><br/><br/>";
}
?>
However, the column "datum" (dutch for date) is varchar. If I want to be able to easily order the results, it's usefull if I change this to the type date. This had the input type of YYYY-MM-DD, for example 2012-08-31.
And here's my problem. Dutch poeple read the date in a other format, namely DD-MM-YYYY. And that's why I need a code that changes the result from YYYY-MM-DD to DD-MM-YYYY. And then echoes the correct format that is readable for the Dutch people
Might I suggest making the field an unsigned int and storing the date and time in UNIX time format? You could then use the date() function to display the date in whichever format you wish, or even leave it up to users' preference.
I vote for not using the DB engine as a "text formatter" if the formatted text is not used somehow in the query itself - use PHP date format functions.
Putting the date formatting into the DB violites the MVC principles IMHO (i.e. parts of the View layer are moved into the Model layer)
There are 10 types of people in this world, those who understand binary and those who don't
You could use Celauran's suggestion, though I don't like putting dates into timestamp format - many of the Date&Time DB functions won't work with it (and "typecasting" would not use indexes if any).
<?php
require_once("connect.php"); //Voor de connectie met de database.
$select = "SELECT * FROM agenda ORDER BY datum DESC LIMIT 2";
$query = mysql_query($select)or die(mysql_error());
while($list = mysql_fetch_object($query)){
$date = date_create_from_format('Y-M-D', 'datum');
echo "<b>Datum: </b>date_format($date, 'd-m-Y');<br/>";
echo "<b>Locatie: </b>$list->locatie<br/>";
echo "<b>Plaats: </b>$list->plaats<br/>";
echo "<img src=\"images/line-leftbar.jpg\"><br/><br/>";
}
?>
I know this isn't working, because I get a fatal error on: $date = date_create_from_format('Y-M-D', 'datum');
I think I cannot put 'datum' there. I've tried this:
When using date_create_from_format, you need to specify the current format. The fatal error is probably a result of a mismatch there. Check how the dates are stored in your database.
I get a fatal error when I try it out:
Fatal error: Call to undefined function date_create_from_format() in /home/********/public_html/index.php on line 59
Does it say that it doesn't know the function of date formatting?