-1

The result of the below code gives: Januar 9, 2014

How is it possible to make it to: 9. Januar 2014

Thanks in advance.

<?php
    global $months;
    $months = array('Januar', 'Februar', 'Marts', 'April', 'Maj', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'December');

    function convert_dates($text) 
    {
        $date_regexp = '/([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})/';
        preg_match_all($date_regexp, $text, $dates);
        $dates     = $dates[0];
        $new_dates = $dates;

        for($i=0;$i<count($new_dates);$i++) {
            $new_dates[$i] = explode('-', $new_dates[$i]);
            $new_dates[$i] = $GLOBALS['months'][abs($new_dates[$i][1])-1] . ' ' . abs($new_dates[$i][2]) . ', ' . $new_dates[$i][0];
        }

        $text = str_replace($dates, $new_dates, $text);
        return $text;
    }
?>
RedHawkDK
  • 119
  • 1
  • 4
  • 9
  • Its not quite what I was looking for. I want to use the above code, but I have to switch the number (day) and the text (month). – RedHawkDK Jan 09 '14 at 15:06
  • This isn't duplicate. This is quite different. –  Jan 09 '14 at 15:07
  • Try with `echo date('j. F Y');` (altrhought month names are on English) –  Jan 09 '14 at 15:09
  • @ZDroid Your suggestion gives me the right result, but I already have a date stored in a database. I receive the date from the database and store it in a variable. I wan't to echo that variable with the right format. Hope it makes sense. – RedHawkDK Jan 09 '14 at 15:11
  • I know, but you don't need to store date in database. PHP makes it for you. :) –  Jan 09 '14 at 15:12
  • Ya that's right. But lets say I have a date in the database, eg. 3 years ago and I just received the date from the database. How would I then display it in the correct format ? – RedHawkDK Jan 09 '14 at 15:22
  • You don't need to worry about displaying it. Just save it on right time as `date(...)`, and then you'll be fine. –  Jan 09 '14 at 15:35
  • I thought about that too, and I know how to do that. But I have read that it is not the correct way to do it. The date format should be the standard one on the mysql database, and then I should format the date on the page when I need the date. The code I posted in my question does that, I just need to change the position. :) – RedHawkDK Jan 09 '14 at 15:42

1 Answers1

0

Use strftime to format the date if you don't want English, and strtotime to convert from Y-m-d format to a timestamp.

<?php
// this needs to happen before you call convert_dates
// If your system default is already correct you do not need to do this
setlocale(LC_ALL, 'da_DK.UTF-8');

function convert_dates($text) 
{
    $date_regexp = '/([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})/';
    preg_match_all($date_regexp, $text, $dates);
    $dates     = $dates[0];
    $new_dates = $dates;

    for($i=0;$i<count($new_dates);$i++) {
        $new_dates[$i] = strftime('%e. %B %Y', strtotime($new_dates[$i]));
    }

    $text = str_replace($dates, $new_dates, $text);
    return $text;
}
mcrumley
  • 5,682
  • 3
  • 25
  • 33