user6200208
user6200208

Reputation:

Date PhP Localized

I'm trying to write a function to display my date separated in French with the full name.

My post_meta (wordpress) return this :

array (size=3)
'date' => string '09/02/2017' (length=10)
'hours' => string '00' (length=2)
'minutes' => string '00' (length=2)

It's in french format d/m/y, I have tried strftime() with argument %B for locale full month display but I don't understand this function, it needs complete format date.

For example, when I type

$month = 02;
echo strftime("%B", $month);

the function return always "Janvier" (January in english)..

Anyone I have an idea for fix this and display day of week (letter) / day (number) and month (letter)?

Thanks.

Upvotes: 0

Views: 92

Answers (3)

user6200208
user6200208

Reputation:

Thanks for your help !

I have written my function with first method and it's working :)

I post the function if this can help anyone in future !

function wb_get_event_date ( $format )
{
    global $post;
    setlocale (LC_TIME, 'French');
    $event_date = get_post_meta($post->ID, 'wb_agenda_date', true);
    $event_date_explode = explode("/", $event_date['date']);
    $tim = mktime(0, 0, 1, $event_date_explode[1], $event_date_explode[0], $event_date_explode[2]);

    switch ( $format ) {
        case 'day_full':
            echo utf8_encode(strftime("%A", $tim));
            break;
        case 'day':
            echo strftime("%d", $tim);
            break;
        case 'month':
            echo utf8_encode(strftime("%B", $tim));
            break;
        case 'year':
            echo strftime("%Y", $tim);
            break;
    }
}

Upvotes: 0

RiggsFolly
RiggsFolly

Reputation: 94642

strftime() requires a timestamp not just a number.

echo strftime("%B", time()+(60*60*24*10));  // now + 10 days to get into February

Or

$day = 1;
$month = 2;
$year = 2017;

$time = mktime(0, 0, 1, $month, $day, $year);
echo strftime("%B", $time);

Upvotes: 1

OscarAkaElvis
OscarAkaElvis

Reputation: 5714

Try this:

<?php
$var = array (
    'date' => '09/02/2017',
    'hours' => '00',
    'minutes' => '00'
);

$date = strtotime(substr($var['date'], 3, 2).'/'.substr($var['date'], 0, 2).'/'.substr($var['date'], 6, 4));

echo date('l', $date); //day of the week in letters format. It produces: Thursday
echo '<br/>';
echo date('d', $date); //day number format. It produces: 09
echo '<br/>';
echo date('F', $date); //month letters format. It produces: February

Upvotes: 0

Related Questions