Unusual behavior of some of my code using strftime ()

I get some dates from the database and use PHP strftime to format them.

Now everything works as planned, in addition, if I use the %A format, which should give me the full name of the day of the week, the function simply returns NULL if the date is not on the weekend, in which case it correctly returns “Saturday” or sunday.

All other formats work, even %A (short name of the day of the week). This does not seem to depend on the language used or on the specific date format (the same problem occurs if I just use strftime on mktime .

My only thought is that this is some incredibly strange server configuration problem, but I would like to hear if anyone has any other ideas about this ...

EDIT: some code, although this is pretty much what I wrote before ...

 $id = (int)$_GET['event']; $res = mysql_query("SELECT date FROM events WHERE event_id=".$id); $row = mysql_fetch_array($res); echo strftime("%a %H:%M", $row['date']); echo strftime("%A %H:%M", $row['date']); 

The first echo works fine, returning, for example, Thu 15:30 , the second returns NULL if $row['date'] does not fall on Saturday or Sunday.

If this can be useful, the code is inside the class, but I don’t see how this can affect the result ...

EDIT2: this is NOT a problem with the database or date format, otherwise the first echo will not work. Alternatively, I can simply delete the DB code and generate the date using mktime or using strtotime , and it still doesn't work.

EDIT3 (solution): found a problem. In Italian, the names of the days end with "(for example, lunedì), except Saturday and Sunday, which do not have accented letters. The result of the operation was passed to json_encode , which apparently does not like accented characters ... Call utf8_encode (or htmlentities ) solved a problem.

+4
source share
2 answers

According to the manual: http://php.net/manual/en/function.strftime.php

If you transmit something other than a timestamp, you are doing it wrong. I can’t say why the first passes, but the second does not. Perhaps PHP is trying to compensate. In any case, if you have a textual representation of time, you first need to call strtotime() .

EDIT

I ran the following code on my system

 $row['date'] = '2011-04-06 08:33:29'; echo strftime("%a %H:%M", $row['date']); echo '<br>'; echo strftime("%A %H:%M", $row['date']); 

And I got it as a conclusion

 Notice: A non well formed numeric value encountered in F:\webroot\utils\test.php on line 4 Thu 00:33 Notice: A non well formed numeric value encountered in F:\webroot\utils\test.php on line 6 Thursday 00:33 

Notifications must be enabled on your system. Changing it to a timestamp should solve it.

EDIT 2

... Alternatively, I can simply delete the DB code, and generate the date using mktime or with strtotime, and it still doesn't work

If you can post a sample that doesn't work, we could take a look

+4
source

In your comments, you say that your database contains dates, such as 2011-04-06 08:33:29 . But the second argument to strftime must be a unix timestamp, for example 1302766547 , that is, the number of seconds since 1970-01-01 00:00:00 GMT . Try instead:

 echo strftime('%a %H:%M', strtotime($row['date'])); 
+3
source

Source: https://habr.com/ru/post/1348045/


All Articles