PeggyBuckham Posted October 8, 2013 Share Posted October 8, 2013 I have a script that, along with other data, uploads a date into database. The date is generated using the date() function. date('m/d/y'.time()); What gets put into the data base is: 0.1098901098461 If I echo out the date like this echo '<p>'.date('m/d/y'.time());.'</p>'; then I get the correct results: 10/8/13 Recently, after turning on php error messages on the server, I got an error (don't remember what it was exactly). I got rid of the error by putting this code in the header date_default_timezone_set('MST'); I've never had to set the default timezone before and I've never had problems with the date() function, but I know that someone out there can help me. Quote Link to comment https://forums.phpfreaks.com/topic/282814-date-function-not-working-in-query/ Share on other sites More sharing options...
B_CooperA Posted October 8, 2013 Share Posted October 8, 2013 (edited) What datatype are u using in your database? If you're using a timestamp, it saves the date in this format: date("Y-m-d G:i:s"); The best practice is to always save the date and time in format I mentioned above. When you're fetching it back, you can modify the output by converting the string to time like so: date("m/d/Y", strtotime($row['YOUR_DATE'])); Edited October 8, 2013 by B_CooperA Quote Link to comment https://forums.phpfreaks.com/topic/282814-date-function-not-working-in-query/#findComment-1453119 Share on other sites More sharing options...
Solution Barand Posted October 8, 2013 Solution Share Posted October 8, 2013 (edited) You should store dates in Y-m-d format. As you want the current date the easiest way is to use a TIMESTAMP type column then you can forget about it in your insert query as it will auto-update. Alternatively you can put CURDATE() in your insert query and use DATE type column. The reason you get the result you do is that your date wasn't in single quotes so it took it to be 10 / 8 / 13 (ten divided by eight divided by thirteeen) Edited October 8, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/282814-date-function-not-working-in-query/#findComment-1453124 Share on other sites More sharing options...
PeggyBuckham Posted October 8, 2013 Author Share Posted October 8, 2013 The reason you get the result you do is that your date wasn't in single quotes so it took it to be 10 / 8 / 13 (ten divided by eight divided by thirteeen) lol ... Now that was a careless move wasn't it! Thanks so much!! I also have another field in the database that has the timestamp in it. I always put a timestamp on almost everything. I just wanted a column with the date in a string so that my client could read it when the data was output onto a spreadsheet. Thanks Again! Quote Link to comment https://forums.phpfreaks.com/topic/282814-date-function-not-working-in-query/#findComment-1453128 Share on other sites More sharing options...
Barand Posted October 8, 2013 Share Posted October 8, 2013 Format the date or timestamp when you extract from the table to the spreadsheet SELECT DATE_FORMAT(mydatecol, '%m/%d/%y') as pretty_date FROM mytable Quote Link to comment https://forums.phpfreaks.com/topic/282814-date-function-not-working-in-query/#findComment-1453132 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.