/    Sign up×
Community /Pin to ProfileBookmark

date conversion

hi,

i have news with a date field (0000-00-00) stored in my db.

for a blog-like archive sidebar i want to list all the months where a news article is available, like

March 2006
January 2006
December 2005
….

to convert the date data i use

[code=php]
$date = mysql_result($result, “date”);

function dateconvert($date,$func) {
//….
if ($func == 3){ //output conversion
list($year, $month, $day) = split(‘[-.]’, $date);
$date = “$month $year”;
return $date;
}

$period = dateconvert($date,3);
echo $period;
// this results in an output like
// 03 2006
[/code]

since i want 03 displayed as March i modified the conversion function like this:

[code=php]
if ($func == 3){ //output conversion
list($year, $month, $day) = split(‘[-.]’, $date);
$month = date(“F”, $month);
$date = “$month $year”;
return $date;
}
[/code]

but now i always get “January”, no matter what the month is. the year is still displayed correctly.

any ideas?

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@bokehMar 20.2006 — [code=php]<?php

echo dateconvert('2006-03-29');

function dateconvert($date)
{
if($ts = strtotime($date))
{
return date('F', $ts) . ' ' . date('Y', $ts);
}
return false;
}

?>[/code]
Copy linkTweet thisAlerts:
@jogolauthorMar 20.2006 — thanks a lot bokeh. let´s keep things simple, right? ?
Copy linkTweet thisAlerts:
@bokehMar 20.2006 — keep things simple, right? ?[/QUOTE]Well yes, especially for examples; it makes them easier to follow.
Copy linkTweet thisAlerts:
@jogolauthorMar 20.2006 — another quick question:

what if i want to display the name of the month in german?

setlocale(LC_TIME, 'de_DE.UTF8'); only works with strftime() afaik.
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYMar 20.2006 — well, in bokeh's example $ts is your timestamp, simply apply the german locale to it (with strftime)
Copy linkTweet thisAlerts:
@jogolauthorMar 20.2006 — ok got it:
[code=php]setlocale(LC_TIME, 'de_DE.UTF8');

function dateconvert($date) {

if($ts = strtotime($date)) {
return strftime('%B', $ts) . ' ' . date('Y', $ts);
}
return false;
} [/code]
but although i put

[B]<meta http-equiv="Content-type" content="text/html; charset=utf-8" />[/B]

into the head of my document the browsers won´t display the umlaut correctly ?

the page displays in ISO-8859-1

März 2006
Copy linkTweet thisAlerts:
@bokehMar 20.2006 — Can you provide a link? Also use [I]iso-8859-1[/I] as the charset of the document. [code=php]<?php

echo dateconvert('2006-03-29');

function dateconvert($date)
{
if($ts = strtotime($date))
{
setlocale(LC_TIME, 'de');
return strftime("%B %Y", $ts);
}
return false;
}

?>[/code]
Copy linkTweet thisAlerts:
@jogolauthorMar 20.2006 — i removed [B].UTF8[/B] from the setlocale(). now it works. thx for your help guys.
Copy linkTweet thisAlerts:
@bokehMar 20.2006 — [URL=http://bokehman.com/umlaut.trouble]Umlaut trouble... iso-8859-1 works fine for me[/URL]
×

Success!

Help @jogol spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...