开发者

Bizarre behaviour of some of my code using strftime()

I'm pulling some dates from a DB and using PHP strftime to format them.

Now, everything works as intended, apart that if I use the %A format, which is supposed to give me the full weekday name the function just returns NULL, unless the date happens to be on a weekend, in which case it correctly returns "Saturday" or "Sunday".

All the other formats work, even %a (short weekday name). It does not seem to depend on the locale I use, nor on the specific format of the date (same problem happens if 开发者_StackOverflow中文版I just use strftime on mktime.

My only thought is that it's some sort of incredibly weird configuration problem server side, but I'd like to hear if anyone had other ideas about it...

EDIT: some code, although it's pretty much what I have written 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 instance Thu 15:30, the second returns NULL unless $row['date'] falls on a Saturday or Sunday.

If this may be of any help the code is inside a class, but I can't see how this may affect the result...

EDIT2: This is NOT a problem with the DB or the date format, otherwise the first echo wouldn't work. Also, I can just remove the DB code, and generate the date with mktime or with strtotime and it still doesn't work.

EDIT3 (solution): Found the issue. In Italian, the names of the days end in ì (e.g. lunedì), apart from Saturday and Sunday, which do not have accented letters. The result of the operation was passed to json_encode which apparently doesn't like accented characters... A call to utf8_encode (or to htmlentities) solved the issue.


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

If you're passing something other than a timestamp you're doing it wrong. Can't really say why the first one passes and the second one doesn't. Maybe PHP is trying to compensate. In any case, if you have a text time representation, you need to call strtotime() on it first.

EDIT

I ran the following code in 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 this as the output

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

You should have notices enabled on your system. Changing it to timestamp should solve it.

EDIT 2

...Also, I can just remove the DB code, and generate the date with mktime or with strtotime and it still doesn't work

If you could post the sample that doesn't work we could have a look


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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜