|
Posted by Gordon Burditt on May 14, 2009, 5:05 pm
Please log in for more thread options
show/hide quoted text
>>> "SELECT * FROM table WHERE EXTRACT(YEAR FROM `dt`) < 1971 ORDER BY dt ASC";
>>
>> 1. Don't do SELECT *. SELECT what you need. Trust me.
>> 2. I'd do WHERE YEAR(dt) < 1971. No idea though if it gives you any
>> performance or other advantages.
>>
>> If PHP's date type cannot handle '0' days or months (apparently it
>> can't), use a simple explode:
PHP's strtotime() will correct the date for out-of-range values.
Day 0 is one day before Day 1, so you get the last day of the
previous month. Day 37 is 6 days after a 31-day month, so you get
day 06 of the next month.
Also, PHP's date type uses UNIX time stamps, so keep dates that might
be before 1970 OUT of that format. That especially includes '0000-00-00'
if you're trying to detect that.
show/hide quoted text
>>
>> $dt = "2009-00-00";
>> $date = explode("-", $dt);
>>
>> Then, $date[0] will hold "2009", $date[1] "00" and $date[2] "00". With
>> intval() you can cast and do arithmetic later.
>Thanks Bart. I tried your suggestion and I can get it to format the way
>that I want. However, in the cases where $date[2] is equal to 00, the
>month is printing out as one month earlier than it should.
If you absolutely MUST feed this to date() or strtotime, fix the
day number to 01 (or any valid day for that month) first.
show/hide quoted text
>Example: date coming directly out of the database is 1971-11-00. When
>it is printed, it is coming out as 1971 October. Here's what I used to
>manipulate it:
>$dt = $row["dt"];
>$date = explode("-", $dt);
>//If array has a number for year and month but not day:
>if (($date[1] != 00) && ($date[2] == 00)) {
> $title1 = date('Y F', strtotime($row["dt"]));
Then change $date[2] to 01, implode it, and feed THAT to strtotime.
show/hide quoted text
>}
>//If array has no number month and thus cannot have a number for day:
>if ($date[1] == 00) {
> $title1 = $date[0];
>}
>//If array has a number for year and month and day:
>if (($date[1] != 00) && ($date[2] != 00)) {
> $title1 = date('Y F d', strtotime($row["dt"]));
>}
>Where did I make an error to cause the month displayed to be wrong? I
strtotime() is doing what it is supposed to do.
If you want to stay in the same month, attach a valid day first.
show/hide quoted text
>used strtotime because I could not get $date[1] to format correctly
>(using full month names) any other way.
|
|
Posted by JackM on May 15, 2009, 8:57 am
Please log in for more thread options
Gordon Burditt wrote:
show/hide quoted text
>
>>> $dt = "2009-00-00";
>>> $date = explode("-", $dt);
>>> Then, $date[0] will hold "2009", $date[1] "00" and $date[2] "00". With
>>> intval() you can cast and do arithmetic later.
>> Thanks Bart. I tried your suggestion and I can get it to format the way
>> that I want. However, in the cases where $date[2] is equal to 00, the
>> month is printing out as one month earlier than it should.
>
> If you absolutely MUST feed this to date() or strtotime, fix the
> day number to 01 (or any valid day for that month) first.
>
>> Example: date coming directly out of the database is 1971-11-00. When
>> it is printed, it is coming out as 1971 October. Here's what I used to
>> manipulate it:
>> $dt = $row["dt"];
>> $date = explode("-", $dt);
>> //If array has a number for year and month but not day:
>> if (($date[1] != 00) && ($date[2] == 00)) {
>> $title1 = date('Y F', strtotime($row["dt"]));
>
> Then change $date[2] to 01, implode it, and feed THAT to strtotime.
>
>> }
>> //If array has no number month and thus cannot have a number for day:
>> if ($date[1] == 00) {
>> $title1 = $date[0];
>> }
>> //If array has a number for year and month and day:
>> if (($date[1] != 00) && ($date[2] != 00)) {
>> $title1 = date('Y F d', strtotime($row["dt"]));
>> }
>> Where did I make an error to cause the month displayed to be wrong? I
>
> strtotime() is doing what it is supposed to do.
> If you want to stay in the same month, attach a valid day first.
Thanks Gordon. That explained a whole lot. It appears that since I do
have some pre-1970 dates and I don't want to display unknown days as 01
that using the date options of either PHP or mySQL won't do what I want.
It appears that my best option will be to split the date into three
fields in the database, one using the mySQL YEAR type and the other two
using integers that I can manipulate with if statements. I put a test
together using this and it is working the way that I want it to.
I appreciate the assistance from all in helping me reach a solution to
the problem.
|
|
Posted by Jerry Stuckle on May 15, 2009, 9:11 am
Please log in for more thread options JackM wrote:
show/hide quoted text
> Gordon Burditt wrote:
>>>> $dt = "2009-00-00";
>>>> $date = explode("-", $dt);
>>>> Then, $date[0] will hold "2009", $date[1] "00" and $date[2] "00". With
>>>> intval() you can cast and do arithmetic later.
>>> Thanks Bart. I tried your suggestion and I can get it to format the
>>> way that I want. However, in the cases where $date[2] is equal to 00,
>>> the month is printing out as one month earlier than it should.
>> If you absolutely MUST feed this to date() or strtotime, fix the
>> day number to 01 (or any valid day for that month) first.
>>> Example: date coming directly out of the database is 1971-11-00.
>>> When it is printed, it is coming out as 1971 October. Here's what I
>>> used to manipulate it:
>>> $dt = $row["dt"];
>>> $date = explode("-", $dt);
>>> //If array has a number for year and month but not day:
>>> if (($date[1] != 00) && ($date[2] == 00)) {
>>> $title1 = date('Y F', strtotime($row["dt"]));
>> Then change $date[2] to 01, implode it, and feed THAT to strtotime.
>>> }
>>> //If array has no number month and thus cannot have a number for day:
>>> if ($date[1] == 00) {
>>> $title1 = $date[0];
>>> }
>>> //If array has a number for year and month and day:
>>> if (($date[1] != 00) && ($date[2] != 00)) {
>>> $title1 = date('Y F d', strtotime($row["dt"]));
>>> }
>>> Where did I make an error to cause the month displayed to be wrong? I
>> strtotime() is doing what it is supposed to do.
>> If you want to stay in the same month, attach a valid day first.
>
> Thanks Gordon. That explained a whole lot. It appears that since I do
> have some pre-1970 dates and I don't want to display unknown days as 01
> that using the date options of either PHP or mySQL won't do what I want.
>
> It appears that my best option will be to split the date into three
> fields in the database, one using the mySQL YEAR type and the other two
> using integers that I can manipulate with if statements. I put a test
> together using this and it is working the way that I want it to.
>
> I appreciate the assistance from all in helping me reach a solution to
> the problem.
Not necessary. Just keep it as a DATE field in MySQL (which has
multiple advantages) and explode() like I showed you earlier in PHP.
Much less overhead and easier to handle.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
|
|
Posted by Gordon Burditt on May 15, 2009, 4:49 pm
Please log in for more thread options show/hide quoted text
>Thanks Gordon. That explained a whole lot. It appears that since I do
>have some pre-1970 dates and I don't want to display unknown days as 01
>that using the date options of either PHP or mySQL won't do what I want.
You don't have to use that "01", just put it in there to get the month
name correctly.
Don't paint *MySQL* date functions with the same problems as PHP date
functions. MySQL date functions have no 1970 or 2038 limit. A year
can go from 0000 to 9999.
show/hide quoted text
>It appears that my best option will be to split the date into three
>fields in the database, one using the mySQL YEAR type and the other two
>using integers that I can manipulate with if statements. I put a test
>together using this and it is working the way that I want it to.
I don't see why you can't use year(field), month(field), and day(field),
then manipulate the results as strings with if statements.
|
|
Posted by =?ISO-8859-1?Q?=22=C1lvaro_G=2 on May 14, 2009, 3:32 am
Please log in for more thread options JackM escribió:
show/hide quoted text
> Álvaro G. Vicario wrote:
>> JackM escribió:
>>> I need a little direction in manipulating a date being pulled from a
>>> mySQL db. It's not something I usually need to do so I am at a loss
>>> on how to accomplish it.
>>> The date is stored in the yyyy-mm-dd format in a field named dt that
>>> is of the date type. Some of the entries are entered as 1970-00-00 or
>>> 1970-05-00 where an actual month and/or day are unknown. I am trying
>>> to manipulate those dates to drop the zeros and only print out the year.
>> A little hint:
>> SELECT
>> '2009-05-00',
>> YEAR('2009-05-00'),
>> MONTH('2009-05-00'),
>> DAY('2009-05-00'),
>> IF(DAY('2009-05-00')=0, YEAR('2009-05-00'), '2009-05-00')
>
> Are you saying that I need to run a second SELECT statement for just the
> date manipulation? I won't have the exact 2009-05-00 information to do
> that with each entry.
Nope. I'm suggesting that one of the possibilities is fetching the data
you need in the first place, rather than using a different format and
then transform it. My code was a sampler of some MySQL functions that
could help. Is it a strange approach?
--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com -- Mi web de humor satinado: http://www.demogracia.com --
|
| Similar Threads | Posted | | Help with comparing date stamp in PHP to date AND time stamp coming from MYSQL db!!!!!! | May 8, 2008, 1:21 am |
| displaying a pdf coming out of a mysql-db in a frame | October 8, 2004, 12:31 pm |
| Date is not being pulled from MYSQL database, instead current date is displayed!!! | March 22, 2006, 12:29 pm |
| Put today's date into a MySQL "date" formated field | August 3, 2006, 3:47 pm |
| manipulating forms | December 12, 2006, 9:07 pm |
| Manipulating binary data | April 29, 2007, 12:36 pm |
| manipulating multiple browser windows | September 9, 2004, 4:44 pm |
| reading and manipulating vcalendar files | June 12, 2005, 8:00 am |
| Manipulating an object returned from function | July 13, 2007, 12:11 pm |
| POST variables not coming through | August 29, 2006, 11:33 am |
|
>>
>> 1. Don't do SELECT *. SELECT what you need. Trust me.
>> 2. I'd do WHERE YEAR(dt) < 1971. No idea though if it gives you any
>> performance or other advantages.
>>
>> If PHP's date type cannot handle '0' days or months (apparently it
>> can't), use a simple explode: