php/mysql date display problem
Moderator: General Moderators
-
theperfectdrug
- Forum Newbie
- Posts: 24
- Joined: Tue May 25, 2004 10:15 am
php/mysql date display problem
Hi!
I'm making articles section using php/mysql; so I've got a problem in displaying dates;
In some of the articles I don't have complete date, like in date(DATE type 0000-00-00) field there is only a year or year and month; some of them are complete(year-month-day).
I'm using following query to format dates:
$sql = "select headline, body, sid, date_format(date, '%M %e, %Y') as fdate from articles order by date desc";
So dates look like this(in case if date is complete year-month-day):
EX: October 3, 2002
But if date is not complete (month-year) script returns:
EX: October 0, 2002
In case when there is only a year in date field (2002-00-00) it shows no date at all.
I just want script to return complete date when it's available, when there is only -month and year- in date field I want it to return date in following format, EX: October, 2002 instead of October 0, 2002...In case if there is only a year in Date field EX: 2002; I want script to show year, instead of empty space.
I've tryed a few methods, in all cases if I managed to format date (month day, year) or (month, year), I'm still getting empty space instead of year.
Any ideas?
Thanks
I'm making articles section using php/mysql; so I've got a problem in displaying dates;
In some of the articles I don't have complete date, like in date(DATE type 0000-00-00) field there is only a year or year and month; some of them are complete(year-month-day).
I'm using following query to format dates:
$sql = "select headline, body, sid, date_format(date, '%M %e, %Y') as fdate from articles order by date desc";
So dates look like this(in case if date is complete year-month-day):
EX: October 3, 2002
But if date is not complete (month-year) script returns:
EX: October 0, 2002
In case when there is only a year in date field (2002-00-00) it shows no date at all.
I just want script to return complete date when it's available, when there is only -month and year- in date field I want it to return date in following format, EX: October, 2002 instead of October 0, 2002...In case if there is only a year in Date field EX: 2002; I want script to show year, instead of empty space.
I've tryed a few methods, in all cases if I managed to format date (month day, year) or (month, year), I'm still getting empty space instead of year.
Any ideas?
Thanks
- launchcode
- Forum Contributor
- Posts: 401
- Joined: Tue May 11, 2004 7:32 pm
- Location: UK
- Contact:
-
theperfectdrug
- Forum Newbie
- Posts: 24
- Joined: Tue May 25, 2004 10:15 am
- launchcode
- Forum Contributor
- Posts: 401
- Joined: Tue May 11, 2004 7:32 pm
- Location: UK
- Contact:
You could just do it like this?
Which would give you 3 new columns from which you can work out on a code level what to do?
Code: Select all
select date,
date_format(date, '%Y') as year,
date_format(date, '%m') as month,
date_format(date, '%d') as day
from dates-
theperfectdrug
- Forum Newbie
- Posts: 24
- Joined: Tue May 25, 2004 10:15 am
- launchcode
- Forum Contributor
- Posts: 401
- Joined: Tue May 11, 2004 7:32 pm
- Location: UK
- Contact:
-
theperfectdrug
- Forum Newbie
- Posts: 24
- Joined: Tue May 25, 2004 10:15 am
-
theperfectdrug
- Forum Newbie
- Posts: 24
- Joined: Tue May 25, 2004 10:15 am
-
theperfectdrug
- Forum Newbie
- Posts: 24
- Joined: Tue May 25, 2004 10:15 am
-
theperfectdrug
- Forum Newbie
- Posts: 24
- Joined: Tue May 25, 2004 10:15 am
I wrote lil' code, with if/else stuff, it works fine but I'm getting ',' before year (In case when there's only year in Date column EX: 2002-00-00)
Here's Example:
, 2002
is there any way to fix this problem?
Code: Select all
if ($day == ""){
echo "$month, $year";
}
elseif ($month == "" || $day == ""){
echo "$year";
}
else{
echo "$month $day, $year";
}, 2002
is there any way to fix this problem?
-
theperfectdrug
- Forum Newbie
- Posts: 24
- Joined: Tue May 25, 2004 10:15 am
-
theperfectdrug
- Forum Newbie
- Posts: 24
- Joined: Tue May 25, 2004 10:15 am