SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast pff's Avatar
    Join Date
    Dec 2000
    Location
    Google PageRank: 6
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Date Manipulation In PHP

    Hello,
    I want to display the month names in another language. I've seen some examples of changing the names of the weekdays, but I couldn't find one with changing the names of the months. Any help will be appreciated.

    Thanks
    pff | I am not what I am

    'Does he exist in the same way as I exist?'
    'You do not exist Winston,' said O'Brien.

  2. #2
    SitePoint Wizard
    Join Date
    Jul 2001
    Location
    The Netherlands
    Posts
    2,617
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Let me post a large piece of text, part of an article I wrote about this subject. Here it goes:

    Showing names of weekdays and months in a language other than English

    As you might have noticed, PHP only used the English names and standards to display the date and time. But what if your are building, or thinking to implement this code on, a website in French or German? It looks like a real disappointment, since it seems impossible to alter pre-set functions, like the date()-function and it’s parameters.

    But what if I told you there is a way? That would really lighten up your day, wouldn’t it? So, pull a smile on your face and read on, because I am going to show you how to pull that stunt.

    First, we need the code that we have written in the previous parts of this article. We will use this code to start off with, and we will then alter it. I will be using French as an example-language, but this could just as well be done with all other languages.

    First of all, let’s recall the code we have written throughout this article:

    <?php

    $time = date(“H:i:s”);
    $date = (“l, F jS, Y”);

    echo ($time);
    echo $date;

    ?>

    What we will do now is, we will add two arrays. These arrays will contain the names of weekdays and months in French. Then, we will read the names out of these arrays, and force PHP to use them to display them, in stead of the English names.
    Let’s see where to put the arrays in the code:

    <?php

    $day_array = array(
    “Dimanche, “Lundi”, “Mardi”, “Mercredi”, “Jeudi”, “Vendredi”, “Samedi”);

    $month_array = array(
    “Janvier”, “Fevrier”, “Marche”, “Avril”, “May”, “Juin”, “Juillet”,
    “Août”, “Septembre”, “Octobre”, “Novembre”, “Decembre”);

    $time = date(“H:i:s”);
    $date = (“l, F jS, Y”);

    echo ($time);
    echo $date;

    ?>

    Not so hard, isn’t it? Well, now we are going to perform the difficult part, forcing PHP to use our self-made arrays to display the names of the weekdays and months. Let me explain a bit. We can tell PHP to grab a value out of an array, and we will combine this with the date()-function. This will make PHP use the names that we just have put in our arrays.
    Now, let’s get on with the coding stuff:
    <?php

    $day_array = array(
    "Dimache", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi");
    $day = $day_array[date("w")] . ", ";

    $month_array = array(
    "Janvier", "Fevrier", "Marche", "Avril", "May", "Juin", "Juillet",
    "Août", "Septembre", "Octobre", "Novembre", "Decembre");
    $month = $month_array[date("n")-1] . " " . date("j, ") . date(Y);

    echo $day, $month;


    ?>

    Freaky, isn’t it? What we have done just now is quite simple. Because PHP uses digits to identify the days and months, we told PHP to grab a string of text out of the array, which has the same value as the current day or month, and display that value in stead of the English name for the current day or month.
    I will explain this to you with an example, since the above explanation might be a bit hard to understand. Let’s say, this month is July. In PHP, this month is identified with the 7. Now, we don’t want PHP to use the name July, we want it to use Juillet. In order to do that we used the following piece of code:

    $month = $month_array[date("n")-1] . " " . date("j, ") . date(Y);

    The last part of it, isn’t important. That part shows the current day of the month and the year. The first part however, is the tricky one. We told PHP that the array $month_array, was the base for the output of the names of the month. So, when PHP identifies this month as being July, it will look into the array for a string of text (the name of the month) with the same value as this month. Because the first object in an array is always identified by 0, the –1 part was needed. This because Janvier was assigned a 0, and therefore Decembre was assigned a 11. The –1 part correct this difference and shows the correct month.
    Now, I would like to point out that we had to use the digital-format to identify the month. Because we needed the value assigned to the month, we needed PHP to get that value for us. That value is equal to the number of the month, and thus the parameter “n” was just what we needed.

    This works exactly the same for the day array, the only difference is the absence of –1. This is because Sunday is identified in PHP as 0. Therefore, putting Dimanche at the front of the array, there would be now problems with PHP finding the wrong name for the month.

    The echo-statement will now output, for today, when I wrote this article, Vendredi, Juillet 20, 2001.

    Okay, so in this article I have tried to teach you to use PHP to show the date and time on your website, and alter the names of the weekdays and months, to match the language used on your website.
    I hope you are now able to put this feature of PHP to good use on your website.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •