Jump to content

Date() sometimes randomly returns "1/1/1970"


hadoob024

Recommended Posts

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)?

Link to comment
https://forums.phpfreaks.com/topic/239645-date-sometimes-randomly-returns-111970/
Share on other sites

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?

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.