Any MySQL geeks aro...
 

MegaSack DRAW - 6pm Christmas Eve - LIVE on our YouTube Channel

[Closed] Any MySQL geeks around? 🙂

12 Posts
6 Users
0 Reactions
48 Views
Posts: 8177
Free Member
Topic starter
 

Bear with me cos I'm learning!

I have an issue with dates being converted to all zeroes. I have a datetime field in one of my tables which should be populated by a date coming in from a web app. The date/time coming in is in the format 7/17/2010 9:18:18 (for example). I assume it's because MySQL was expecting the date in a certain format, and I'm giving it the wrong one. I tried changing the format of the datetime_format variable, but it doesn't help. Google is throwing up some confusing results!

Any idea wise ones? 🙂


 
Posted : 16/07/2010 11:39 am
Posts: 0
Free Member
 

MySQL online documentation is really good. It helped me loads during my brief flirtation. I think I had the same problem in fact. Have you looked [url= http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format ]here?[/url]

Also, is your MySQL field a datetime rather than a date?


 
Posted : 16/07/2010 11:43 am
Posts: 1
Free Member
 

I'm a novice too, however if your getting php to write to the database and the date entered is the same date you enter it i.e. is current as opposed to a date in history. If for the date field on the Sql string instead of the variable use 'NOW()' and it'll fill the database with the correct date/time at the time you add it.. if that makes any sense at all (again I'm also a complete novice).


 
Posted : 16/07/2010 11:46 am
Posts: 0
Free Member
 

it expects yyyy-mm-dd hh:mi:ss


 
Posted : 16/07/2010 11:51 am
Posts: 8177
Free Member
Topic starter
 

It is a datetime field.

Steve - I am using PHP. It's a time stamped transaction that's coming in, so I need the actual time rather than current time. I have a timestamp field that is being correctly populated (after I tweaked the format) thus:
$systemtime = date('Y/m/d h:i:s', $currenttime);

But that takes UNIX time and converts it accordingly.


 
Posted : 16/07/2010 11:52 am
Posts: 8177
Free Member
Topic starter
 

simon - I've tried changing what it's expecting (I think!), and setting "ALLOW_INVALID_DATES" but no change. Can I reformat the date string I'm getting?


 
Posted : 16/07/2010 11:54 am
Posts: 0
Free Member
 

Don't you want:-

$systemtime = date('Y-m-d h:i:s', $currenttime);

(note hyphens rather than slashes)


 
Posted : 16/07/2010 12:09 pm
Posts: 8177
Free Member
Topic starter
 

That bit works allthepies so I'm leaving it!

I've just applied the same logic and converted my incoming time to UNIX time and back again, I now get the date populated in my DB.

$newphoneTime = strtotime($phoneTime);
$phoneTime = date('Y/m/d h:i:s', $newphoneTime);


 
Posted : 16/07/2010 12:13 pm
Posts: 0
Free Member
 

First off, check to make sure that you're actually being passed values. 9/10 it turns out that you're not getting values from the source system. Check this by changing the data type of your destination field to varchar(255). It could be that you're recieving values that are yyyy-mm-dd hh.mm.ss.00 so the length of the field could be causing issues. Also try casting the field as a datatime before delivery. define the format before table insertion just to make sure.


 
Posted : 16/07/2010 12:15 pm
Posts: 8177
Free Member
Topic starter
 

Cheers flange - I did that early on to make sure I was getting something (I was). I can see the transaction coming in on the web server logs, and I passed the raw data through the php urldecode function to see what the date looks like. Fixed now I think, though something might be a bit squiffy as the time says 01:.. instead of 13:...


 
Posted : 16/07/2010 12:20 pm
Posts: 0
Free Member
 

>as the time says 01:.. instead of 13:...

That's because the lower case 'h' in the format string is a 12-hour format, change it to an upper case H and you'll get 24-hr.


 
Posted : 16/07/2010 12:24 pm
Posts: 8177
Free Member
Topic starter
 

Aye, just twigged that!

Cheers 🙂


 
Posted : 16/07/2010 12:26 pm
Posts: 0
Free Member
 

... and don't forget timezones and daylight saving 🙂


 
Posted : 16/07/2010 12:26 pm