Azu Posted February 4, 2007 Share Posted February 4, 2007 I used to have dates stored as chars E.G. "07-12-30 10:50:50" for human readability but I switched it to integers so that I can sort by date and so that it runs faster, E.G. 071230105050 The problem is, it keeps cutting off the 0 in front of the 7 for some reason. So I have to store the whole year E.G. 20071230105050 and then it works. I'd like to be able to just use 07 though. Any ideas why the 0 is getting cut off and how I can fix this? Oh and I am storing the integers in bigint(99) so I'm sure it's not that the integer doesn't fit. And also since it works with the full year (2007 instead of 07) so I am sure it's not that the integer is just to big and is getting cut off.. Also is there a function to change the integer into human readable format? I have found many functions that do the EXACT OPPISITE of this (changing human readable format into integer) but nothing to change integer into human readable, so I'm making my own functions to convert it, but it would really be nice if there is already a function in PHP that does this that I just haven't been able to find yet, and if there is can you please tell me what it is? Oh and in case you are wandering the reason I'm storing it in integer format instead of datetime format is that I need to have two of these values and MySQL throws an error when I try to have more then 1 datetime field. Quote Link to comment https://forums.phpfreaks.com/topic/37026-solved-mysql-date-sorting/ Share on other sites More sharing options...
sasa Posted February 4, 2007 Share Posted February 4, 2007 number 071230105050 == 71230105050 if you want 0 in front try $a=071230105050; echo $a,"<br />\n"; $b = sprintf('%012s',$a); echo $b; or use unix timestamp $date = '07-12-30 10:50:50'; echo $x = strtotime($date),"<br />\n"; //date to int (Unix timestamp - number of sec from 1970-1-1) $date1 = date('y-m-d h:i:s',$x); // int to date echo $date1; Quote Link to comment https://forums.phpfreaks.com/topic/37026-solved-mysql-date-sorting/#findComment-176795 Share on other sites More sharing options...
Azu Posted February 4, 2007 Author Share Posted February 4, 2007 Thank you! That is exactly what I was looking for =D Quote Link to comment https://forums.phpfreaks.com/topic/37026-solved-mysql-date-sorting/#findComment-176878 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.