>Having a little trouble with converting dates.
>
>I have, in my database, a bunch of dates stored like this: YYYY-M. Month is
>obviously the number of the month (5), not the name (May).
>
>I want to convert the format to MMM, YYYY (ex: May, 2002), so I used the
>mktime function. Basically I extract the date with a regular MySQL query,
>then explode the date, and my components look great. However, when I put the
>components into my mktime function, sometimes it'll work and sometimes it
>won't. It won't work if the date is too far off in the future (2085, etc.),
>but I'm not exactly sure what the cutoff is. If it's recent, say 2005, it
>works fine.
>
>My code is this (after grabbing the query results):
>$enddate = explode("-", $datereuslt[0]);
>$fullenddate = mktime(0, 0, 0, $enddate[1], 0, $enddate[0]);
>$finaldate = date("M, Y", $fullenddate);
>
>Any ideas? Alternatively, if anyone has an easier way of doing this, I'm all
>ears.

mktime() will not work beyond mid-August of 2038 on 32-bit machines.

This is because Unix time-stamps count time by marking off the number of
seconds since 1/1/1970 midnight:

12:00:00 01/01/1970 == 0
12:00:01 01/01/1970 == 1
12:00:02 01/01/1970 == 2
.
.
.
12:01:00 01/01/1970 == 60
.
.
.
??:??:?? ??/??/2038 == 2147483648 == 0xFFFFFFFF ==
0b11111111111111111111111111111111

After that you either "wrap around" back to 1970, or you buy a 64-bit
machine and extend your time range another century or so.

MySQL *probably* has some data types that can handle much larger dates, and
probably has something like mktime() only different (different name, and
handles bigger dates).

So your best bet is to figure out what MySQL data type will handle the full
range and precision of the dates you need to work with:

http://mysql.com

Another alternative may be as simple as:

$months = array(1=>'January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December');
$enddate = explode("-", $datereuslt[0]);
$month = $enddate[1];
$year = $enddate[0];
$finaldate = $months[$month] . ' ' . $year;

It's crude but it will work for any year no matter how distant.

-- 
Like Music?  http://l-i-e.com/artists.htm


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to