moisesbr Posted August 3, 2013 Share Posted August 3, 2013 Hi I am using command below to get datetime and storing it to varchar type field. $datetime = $today[mday].'/'.$today[mon].'/'.$today[year].' '.$horaReal.':'.$today[minutes].':'.$today[seconds]; I notice that I have error dates with more than 24 hours. e.g: 26:04. Moises Quote Link to comment https://forums.phpfreaks.com/topic/280788-date-with-more-than-24hours/ Share on other sites More sharing options...
Barand Posted August 3, 2013 Share Posted August 3, 2013 Look for where you are putting values greater than 24 into your $horaReal variable. Quote Link to comment https://forums.phpfreaks.com/topic/280788-date-with-more-than-24hours/#findComment-1443327 Share on other sites More sharing options...
jcbones Posted August 3, 2013 Share Posted August 3, 2013 In all honesty, you should be storing dates, datetimes, in either the date, datetime, or timestamp fields. Quote Link to comment https://forums.phpfreaks.com/topic/280788-date-with-more-than-24hours/#findComment-1443368 Share on other sites More sharing options...
moisesbr Posted August 4, 2013 Author Share Posted August 4, 2013 Thanks, I am new to php and a bit confuse. I remember someone had advised me to insert it at varchar field, perhaps because I was not managing to insert it in a datatime field. As a matter of fact, isn't there a ready date_time() function in php as we have in other languages ?Moises Quote Link to comment https://forums.phpfreaks.com/topic/280788-date-with-more-than-24hours/#findComment-1443417 Share on other sites More sharing options...
moisesbr Posted August 4, 2013 Author Share Posted August 4, 2013 Look for where you are putting values greater than 24 into your $horaReal variable. Barand I was look at $horaReal origin and found out the error. $today = getdate(); $num = (int)$today[hours]; $horaReal = $num +4; The + 4 increment is to solve a time difference, but I suppose it does not treat it as expected. Is there a better way to sum time to hour ? Moises Quote Link to comment https://forums.phpfreaks.com/topic/280788-date-with-more-than-24hours/#findComment-1443418 Share on other sites More sharing options...
mac_gyver Posted August 4, 2013 Share Posted August 4, 2013 to perform date/time math you must either handle the overflow from one field to the next in your code, or you need to use a function/method that does this for you. the mktime() function and using the DateTime class will handle date arithmetic with proper overflow. Quote Link to comment https://forums.phpfreaks.com/topic/280788-date-with-more-than-24hours/#findComment-1443424 Share on other sites More sharing options...
jcbones Posted August 4, 2013 Share Posted August 4, 2013 (edited) MySQL has a lot of functions dealing with dates and times. So, if you are storing dates and/or times in MySQL, then you should really store them in the proper column types. Date is for storing ONLY date info. DateTime is for storing dates and times, keeping the data as you insert it. Timestamp converts the data to UTC time, then converts it back to your current timezone upon retrieval. Along with all of these functions http://http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html, it makes no sense to store dates in a varchar field. Edited August 4, 2013 by jcbones Quote Link to comment https://forums.phpfreaks.com/topic/280788-date-with-more-than-24hours/#findComment-1443435 Share on other sites More sharing options...
moisesbr Posted August 5, 2013 Author Share Posted August 5, 2013 I tried: $datetime_in = new DateTime("now"); When try to insert $datetime into datetime field of a mysql table, have error message: Catchable fatal error: Object of class DateTime could not be converted to string in /home/monitorr/public_html/reembolso1.php on line 279 Tried to converto to; $datetime_in->format("Y-m-d H:i:s"); , but there is no point. Quote Link to comment https://forums.phpfreaks.com/topic/280788-date-with-more-than-24hours/#findComment-1443467 Share on other sites More sharing options...
mac_gyver Posted August 5, 2013 Share Posted August 5, 2013 you are going to have to read the php.net documentation for the datetime class before you can use it. there is a point in calling the ->format() method since new DateTime("now") returns an object that you cannot directly use the value of in a sql query statement. Quote Link to comment https://forums.phpfreaks.com/topic/280788-date-with-more-than-24hours/#findComment-1443498 Share on other sites More sharing options...
moisesbr Posted August 5, 2013 Author Share Posted August 5, 2013 I finally managed to insert datetime in a real datetime field in table, using a different approach: date_default_timezone_set('America/Sao_Paulo'); $datetime_in = date('m/d/Y h:i:s a', time()); However time is 2 hours less than Sao Paulo time. Is there any issue regarding date_default_timezone_set fuction ? Moises Quote Link to comment https://forums.phpfreaks.com/topic/280788-date-with-more-than-24hours/#findComment-1443545 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.