2

Hello to everyone i'm new to PHP... with this function i have the name of the days between two dates, but i can't translate it in italian without broken it. Can someone help me? I tried to use strftime() in this two modes, but it gives me the error: strftime() expects parameter 2 to be int, object given, i have done the var_dump of date and it is object, I need to cast it or it is not the answer to my question?

$from_date = new DateTime($from_date);
    $to_date = new DateTime($to_date);

    for ($date = $from_date; $date <= $to_date; $date->modify('+1 day')) {
       echo $date->format('l') . "\n";
       //echo strftime("%A", $date); error : strftime() expects parameter 2 to be int, object given
      //var_dump($date);  object(DateTime)
      }

output: Wednesday Thursday Friday

I pick $from_date and $to_date from :

+------------+------------+
| from_date  | to_date    |
+============+============+
| 2020-04-01 | 2020-04-03 |
+------------+------------+
axel
  • 25
  • 6

2 Answers2

2

This should work for you if you set the locale properly setlocale(LC_TIME, 'it_IT.UTF-8');. I've also modified your code a little bit to make it more readable and concise.

<?php
setlocale(LC_TIME, 'it_IT.UTF-8');
$period = new DatePeriod(new DateTime('2020-04-07'),new DateInterval('P1D'),new DateTime('2020-04-18'));
foreach ($period as $key => $value) {
  echo strftime("%A", $value->getTimestamp()).PHP_EOL;     
}
?>

WORKING DEMO: https://3v4l.org/sYirG

A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
  • I use your format with my variable and new output is: Wednesday Thursday, i have not idea why doesn't works... My code is this : ` $value) { echo strftime("%A", $value->getTimestamp()).PHP_EOL;} ?>` It's really strange because in your example works – axel Apr 18 '20 at 10:11
  • Maybe the problem is xampp? – axel Apr 18 '20 at 11:08
  • @axel You man need to set the header `header("Content-Type: text/html; charset=ISO-8859-1");` also have a look here https://stackoverflow.com/a/15138120/1138192 – A l w a y s S u n n y Apr 18 '20 at 14:10
  • SET_LOCALE documentation: Warning The locale information is maintained per process, not per thread. If you are running PHP on a multithreaded server API like IIS, HHVM or Apache on Windows, you may experience sudden changes in locale settings while a script is running, though the script itself never called setlocale(). This happens due to other scripts running in different threads of the same process at the same time, changing the process-wide locale using setlocale(). – bestprogrammerintheworld Apr 18 '20 at 16:00
0

I fixed it using:

for ($date = $from_date; $date <= $to_date; $date->modify('+1 day')) {
      echo utf8_encode (strftime("%A", $date->getTimestamp())).PHP_EOL;  
     }

output: mercoledì giovedì venerdì

axel
  • 25
  • 6