Date and time in Greek

11,561

Solution 1

I was in the quest to find the same thing but I didn't find a full solution as I needed it. In my case I need to write the posted datetime of the article in Greek like Αναρτήθηκε Σάββατο 2 Μαΐου 2015.

So using some code of costastg answer, I managed to put together the following function.

I am sure there are other solutions out there:

function formatToGreekDate($date){
    //Expected date format yyyy-mm-dd hh:MM:ss
    $greekMonths = array('Ιανουαρίου','Φεβρουαρίου','Μαρτίου','Απριλίου','Μαΐου','Ιουνίου','Ιουλίου','Αυγούστου','Σεπτεμβρίου','Οκτωβρίου','Νοεμβρίου','Δεκεμβρίου');
    $greekdays = array('Δευτέρα','Τρίτη','Τετάρτη','Πέμπτη','Παρασκευή','Σάββατο','Κυριακή');

    $time = strtotime($date);
    $newformat = date('Y-m-d',$time);

    return $greekdays[date('N', strtotime($newformat))-1].' '. date('j', strtotime($newformat)).' '.$greekMonths[date('m', strtotime($newformat))-1]. ' '. date('Y', strtotime($newformat)); // . ' '. $date;
}

Solution 2

The full answer:

date_default_timezone_set('Europe/Athens');

setlocale(LC_TIME, 'el_GR.UTF-8');
echo strftime('%A ');
$greekMonths = array('Ιανουαρίου','Φεβρουαρίου','Μαρτίου','Απριλίου','Μαΐου','Ιουνίου','Ιουλίου','Αυγούστου','Σεπτεμβρίου','Οκτωβρίου','Νοεμβρίου','Δεκεμβρίου');
$greekDate = date('j') . ' ' . $greekMonths[intval(date('m'))-1] . ' ' . date('Y');
echo $greekDate;

this will display the date like:

Πέμπτη 4 Φεβρουαρίου 2013

UPDATE: For the above chunk to work it is very important to set your PHP locale to Greek.

This is an alternative:

date_default_timezone_set('Europe/Athens');

setlocale(LC_TIME, 'el_GR.UTF-8');
$day = date("w");
$greekDays = array( "Κυριακή", "Δευτέρα", "Τρίτη", "Τετάρτη", "Πέμπτη", "Παρασκευή", "Σάββατο" ); 
$greekMonths = array('Ιανουαρίου','Φεβρουαρίου','Μαρτίου','Απριλίου','Μαΐου','Ιουνίου','Ιουλίου','Αυγούστου','Σεπτεμβρίου','Οκτωβρίου','Νοεμβρίου','Δεκεμβρίου');

$greekDate = $greekDays[$day] . ' ' . date('j') . ' ' . $greekMonths[intval(date('m'))-1] . ' ' . date('Y');
echo $greekDate;

Solution 3

You should use setlocale and strftime, an example to get the idea:

setlocale(LC_TIME, 'el_GR.UTF-8');

// Your display formatting from https://www.php.net/manual/en/function.strftime.php
$field_date_format = '%A, %d %B, %Y'; 

// Will display= Greek: Πέμπτη, 14 Φεβρουάριος, 2013
echo "Greek: ".strftime($field_date_format, strtotime("14-02-2013"));

I know "Φεβρουάριος" is not "Φεβρουάριου" but if is not crucial to your needs you can use this.

Share:
11,561
Meserlian
Author by

Meserlian

Updated on June 12, 2022

Comments

  • Meserlian
    Meserlian almost 2 years

    I'm currently using a website to get the time in Athens:

    $d = new DateTime("now", new DateTimeZone("Europe/Athens"));
    echo $d->format("l, d M Y");
    

    But I would like the date to be displayed in Greek and in the same format.

  • themhz
    themhz almost 9 years
    For my server it prints, Tuesday 12 Μαΐου 2015. It seems that the day is not translated.
  • costastg
    costastg about 8 years
    @themis Then try this one: <?php date_default_timezone_set('Europe/Athens'); setlocale(LC_TIME, 'el_GR.UTF-8'); $day = date("w"); $greekDays = array( "Κυριακή", "Δευτέρα", "Τρίτη", "Τετάρτη", "Πέμπτη", "Παρασκευή", "Σάββατο" ); $greekMonths=array('Ιανουαρίου','Φεβρουαρίου','Μαρτίου','Απρ‌​ιλίου','Μαΐου','Ιουν‌​ίου','Ιουλίου','Αυγο‌​ύστου','Σεπτεμβρίου'‌​,'Οκτωβρίου','Νοεμβρ‌​ίου','Δεκεμβρίου'); $greekDate = $greekDays[$day] . ' ' . date('j') . ' ' . $greekMonths[intval(date('m'))-1] . ' ' . date('Y'); echo $greekDate; ?>
  • dionyziz
    dionyziz about 7 years
    This is the correct answer. Then use strftime to format the date according to locale.
  • dionyziz
    dionyziz about 7 years
    This is a poor answer. It requires a reimplementation of date formatting for the specific language. What if I want the site to support 10 languages? Do I have to include all of them in my code literally? That's exactly what locales are for! Duplication of such implementation should be avoided and not be made for all of your web apps on your own.
  • themhz
    themhz about 7 years
    You should consider using resource files in order to use 10 languages. Months can be pronounced differently in different occasions. For example its different to say Φεβρουαρίου and different Φεβρουάριος even if they mean the same month but the usability in the LANGUAGE is different
  • dionyziz
    dionyziz about 7 years
    Correct - but this is the job of a localization library and should not be part of your application. Multiple applications should reuse the same underlying localization library instead of reimplementing it. As for your example with "Φεβρουαρίου" and "Φεβρουάριος", the Unicode Standard specifies different month formats for these: Note that it has two different fields marked as "intended to be used without ‘d’ for day number" and "intended to be used in conjunction with ‘d’ for day number" respectively for exactly this reason.