Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why does strftime() on windows returns false? (i'm not using %e)

Here is my code:

var_dump(strftime("%m-%d-%Y %l:%M:%S", time()));
echo "<br />";
var_dump(strftime("%Y-%d-%m %H:%M:%S", time()));

The first line returns false, and the third returns the anticipated string '2012-10-09 23:03:18' (length=19)

Why is the first line returning false?

I'm running windows 7x64 and wamp with fairly default settings.

like image 204
Alex Waters Avatar asked Sep 11 '12 03:09

Alex Waters


2 Answers

If the first line doesn't work but the second one does, then the reason is obviously because of %l

var_dump(strftime("%l", time()));

Does indeed throw errors on windows.

Reading the manual on strftime...

Not all conversion specifiers may be supported by your C library, in which case they will not be supported by PHP's strftime(). Additionally, not all platforms support negative timestamps, so your date range may be limited to no earlier than the Unix epoch. This means that %e, %T, %R and, %D (and possibly others) - as well as dates prior to Jan 1, 1970 - will not work on Windows, some Linux distributions, and a few other operating systems. For Windows systems, a complete overview of supported conversion specifiers can be found at » MSDN.

Therefore %l is clealy one not supported.

like image 60
fire Avatar answered Nov 14 '22 20:11

fire


While coming across this on my local WAMP server, I found a solution for %l not working. Following the fix of %e, which is %#d, I testing using %#I for an hour without preceding 0 and it worked flawlessly. So you can use this is your code:

var_dump(strftime("%m-%d-%Y %#I:%M:%S", time()));

Unfortunately, this does not work in a linux environment, it will output #I instead of the hour.

like image 27
Andrew Jackman Avatar answered Nov 14 '22 20:11

Andrew Jackman