Jump to content

[SOLVED] Mysql date sorting


Azu

Recommended Posts

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.

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.