Convert strtotime() to StrFTime()
Asked Answered
T

2

6

I was asked to write a module for our billing system and completed it using all of the functions I am accustomed to. It was tested on an English setup, but I did not take into account (nor was I aware of) the fact the actual system our company uses is French.

The code that I wrote is...

$due_date = strtotime($next_due_date);
$today = date('Y-m-d');
$today_date = strtotime($today);

if ($due_date > $today_date) {

However I am unfamiliar with StrFTime and wondering how I can use it to get this code to work for French locale?

Tolbutamide answered 24/1, 2013 at 12:24 Comment(3)
In French locale, what would the $next_due_date contain as a string representation of date?Sundsvall
What exactly is the problem you are facing and what isn't working as it should? Can you show some example data?Metralgia
php.net/manual/en/function.strftime.phpSelima
U
2

First you should use setlocale

setlocale(LC_TIME, "fr_FR");

Second, For compare date as string or int you should still use strtotime and not strftime. (or you can use always string in format YYYY-mm-dd

strftime is only for to represent date as string. If you want store it, or to manipulate it, you should use a standard format and not localized. See doc

To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.

So a possible code

//$next_due_date must be in a standard format like YYYY-mm-dd (but not only)
$due_date = strtotime($next_due_date);
$today_date = time();

if ($due_date > $today_date) {
    echo strftime("%c",$due_date) . " is in the future";
}
Unaware answered 24/1, 2013 at 12:41 Comment(0)
M
-1

Made an simple and quick example. Hope this helps

<?php
        setlocale(LC_ALL, 'fra_fra');

        $today = date('Y-m-d');
        $date_string = utf8_encode(strftime('%d %B %Y', strtotime($today)));
        echo $date_string;
?>
Monastic answered 24/1, 2013 at 12:35 Comment(1)
The second argument of strftime defaults to time(), so theres redundant code in this answer.Kenlee

© 2022 - 2024 — McMap. All rights reserved.