hadoob024 Posted June 17, 2011 Share Posted June 17, 2011 This one's weird. We're using SugarCRM and sometimes we have issues with dates. It's pretty random. It happens about 1 out of every 100 date entries, but here's our code: $bean->status_date = date("m/d/Y H:i") The issue is, that about 1 out every 100 times we do this, sometimes the value "1/1/1970" or "12/31/1969" gets stored in our MSSQL db. I can't see an issue with what I'm doing because it works like 99 times out of 100. But am I doing something wrong? Is this a PHP bug? Is this a SugarCRM bug? Anyone seeing this (aside from when entering bad inputs for the date() function)? Quote Link to comment https://forums.phpfreaks.com/topic/239645-date-sometimes-randomly-returns-111970/ Share on other sites More sharing options...
redixx Posted June 17, 2011 Share Posted June 17, 2011 The date it returns suggests an invalid timestamp. Those dates are the start of the UNIX timestamp. Maybe try echoing time() until it does it, and see what the timestamp is. Quote Link to comment https://forums.phpfreaks.com/topic/239645-date-sometimes-randomly-returns-111970/#findComment-1231027 Share on other sites More sharing options...
hadoob024 Posted June 17, 2011 Author Share Posted June 17, 2011 So it seems that the system is generating an invalid timestamp, right? I guess change my code to something like: $current_time = time(); while ($current_time <= 0) { $current_time = time(); } $good_date = date("m/d/Y H:i", $current_time); Something like that? Quote Link to comment https://forums.phpfreaks.com/topic/239645-date-sometimes-randomly-returns-111970/#findComment-1231037 Share on other sites More sharing options...
The Little Guy Posted June 17, 2011 Share Posted June 17, 2011 you should be storing the date in the database like so: $bean->status_date = date("Y-m-d H:i:s"); Quote Link to comment https://forums.phpfreaks.com/topic/239645-date-sometimes-randomly-returns-111970/#findComment-1231055 Share on other sites More sharing options...
hadoob024 Posted June 17, 2011 Author Share Posted June 17, 2011 I believe that when I'm letting Sugar handle the save through the bean, that I need to have the date in the format of the user's setting. Sugar then knows what the format of the value that it's getting and converts it according to our db format which is "Y-m-d H:i:s". Only when I do a direct UPDATE to the db do I actually put the format in "Y-m-d H:i:s", otherwise I just need to let Sugar know what the date format of the user is. I even created a tmp table where I'm dumping all this info to see what's getting generated and what's getting saved. I set this up: $bean->status_date = date($date_format); $test_query = "INSERT INTO tmp_date_data(proc_id, query, server_name, modified_user, status_id, new_status_id) VALUES('".$bean->id."', '".$bean->status_date."', 'web3', '".$bean->modified_user_id."', '".$row['mystatus']."', '$statusid')"; $test_query_result = $db->query($test_query); And looking at the results it's weird, because in every case (of where the bad date value gets stored), "$bean->status_date" has the correct value, but the value that gets saved is either 1/1/1970 or 12/31/1969. I'm pretty stumped on this one. Quote Link to comment https://forums.phpfreaks.com/topic/239645-date-sometimes-randomly-returns-111970/#findComment-1231062 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.