I want to know if what should I do to change the format of Date FROM YYYY-MM-DD to DD-MM-YYYY using php and also what should i put in the database as date format.
If you store dates as YYYY-MM-DD in your database as a date field then that is good practice - just don’t call your field date as that is a mysql reserved word and will cause you problems when you come to things like
select * from table where YEAR(`date`) = '2011'
UNLESS you quote them with backticks as I just did above, stay away from that jiggery-pokery, call your field join_date or something like that.
(Inside the while loop)
for($i = 0; $i < mysql_num_fields($result); $i++){
Please dont do that. Unnecessary calls to functions will slow your script down. (mysql_num_fields will be called for every field of every row of the function!)
foreach($row AS $value) works fine…
As far as where to insert the date conversion, inside your while loop.
You should store dates in mySQL as “datetime”. You can display them in whatever way that makes sense. There is a PHP function called strtotime() that can convert strings (e.g. 2001-01-01) to PHP timestamps (e.g. 978289200). You can then feed this value to the date() function. Together, this works like this:
If you want it out as 25-05-1990 your date format should be ‘d-m-Y’ rather than ‘Y-m-d’
NOTE: If you’re using a mySQL Date field, the date will always be formatted in ‘Y-m-d’ when stored in the database, and you’ll need to convert it back to your chosen format when retrieving it.
Yes sir my birthday was date field in my database.is it easy for me if I used varchar as my field type in my birthday?so that I dont need to convert it?
In the practical world, you will have to convert at some point. either when the date is entered into the DB or when it’s read. My preferred method is to save as UNIX epoch time to the database. then use the date()function for output and processing.
Anyway it was already mentioned if you have a date stored in a preformated way, YYYY-MM-DD for example, you use strtotime()… that will give you its unix equivalent,
then use date(“j-n-Y”, $yourtimestamp) to get what you want.
the code, broken down for easy comprehension, is:
$dateFromDB;// put your info in this variable
$toCovert=strtotime($dateFromDB);
$toOutput(“j-n-Y”,$toCovert;// “j-n-Y”, is the pattern you want the date output as, in this case DAY-MONTH-YEAR NUMERIC.
echo $toOutput;
this can be done in one line of code, as WDHouston suggested, I just thought I would clear it up.