Try entering your custom dates in the format: yyyy-mm-dd.
Thanks, but that’s unfortunately not an option for me. The date has to be displayed this exact way.
I’ve tried many solutions, but they all turn out the same. I just need some kind of way of telling the loop that the custom field is a date in this format.
Anyone?
Sorry to bump.. I’m just hoping this is possible to do..
The date has to be displayed this exact way.
Just because the date has to be displayed using format A doesn’t mean it can’t be entered as a meta value using format B.
Just because the date has to be displayed using format A doesn’t mean it can’t be entered as a meta value using format B.
okay, thanks! but will the dates still be sorted with format A, even though format b is displayed.
Can you give me an example of how to do this?
thanks again!
If you used ..
02. september 2009
instead of..
2. september 2009
does it make any difference?..
The reason that the dates sort that way is because they sort by the first number, in this case the 2 is greater than the first 1 in say this line..
18. august 2009
does it make any difference?..
Not really, then it just puts it first, because the date starts with a 0. And even if that worked it would still sort the months alphabetically..
Then as Esmi said, use the other date format, then re-arrange the data when you retrieve it.
Then as Esmi said, use the other date format, then re-arrange the data when you retrieve it.
Can you give me an excample on how to re-arrange the data when I retrieve it?
Thanks so much!
That really depends on the format you type the date in… you’d need to test the sort order before commiting time in reformatting it for display, and i don’t have posts that exist like this to test it directly myself.
If you used..
yyyy-mm-dd then you could use explode (can provide an example) to split the data up, then implode (can provide an example) to join it together after resorting it.
I’m not a huge user of date functions, there’s proberly a date function better suited to the job but it’s really not something i’d know without testing.
The point i’d make though is, are you happy with using a different format in the field?
If not, then it’s proberly a waste of time producing an example if you have a distinct requirement to keep the format you originally stated.
Although (thinking out loud) i don’t see why it wouldn’t be possible to resort the posts using an array sorting method before the foreach loop, that way avoiding any need to change the date format.
I’ll go make some test posts…
Esmi said:
Try entering your custom dates in the format: yyyy-mm-dd.
Yes. It is the International date format as per ISO 8601.
S.K
I think that would be best, from my testing (as bad or good as i am, lol) it would take alot of shuffling around code, just to resort the posts.. using your date format.
I think ultimately you’d be creating initial work for the server for very little return…
Where as if you used yyyy-mm-dd when entering the date into the field you could then just reformat the data for display…
mktime and date functions could be used along with explode to switch the display of the date parts.. although i’m not sure if that’s necessarily required.
I think it’s a case of…
$dato = get_post_meta($post->ID, 'Dato', true);
$test = explode('-',$dato);
echo date('j. D, Y', mktime(0,0,0,$test[1],$test[2],$test[0]));
unset($test);
That’s off the top of my head, but i think something along those lines would do it…
That’s off the top of my head, but i think something along those lines would do it…
wow.. That’s working perfectly!! Now they are sorted correctly. The only remaining problem is that the months are now in English. And they have to be Danish.
I looked around the Codex and found this:
<?php
setlocale(LC_ALL, 'da_DK.ISO-8859-15@euro');
echo strftime('%A %d %B, %Y',strtotime(get_the_time('j. D, Y')));
?>
But I don’t really have a clue about where to put it.. All this date stuff is pretty new to me..
Thanks so much again!
Actually I figured it out. I just have to use WP’s date function date_i18n. So the custom field call looks like this:
<?php
$dato = get_post_meta($post->ID, 'Dato', true);
$test = explode('-',$dato);
echo date_i18n('j. F Y', mktime(0,0,0,$test[1],$test[2],$test[0])); unset($test);
?>
Then it displays the date in the right language. So problem solved!! Thanks for all your help t31os_!!!
No problem.. π
You can of course rename the word test to something else, just to make for nicer reading code.. π
You can also safely unset the data from $test (or whatever you name it)
unset($test);
After the date has been echo’ed, if it’s not needed further along..