Really weird behaviour when printing a date with php and drupal
So i'm just trying to print day month and year (not printing actually, i'll compare then with some dates that are in the database, but printing is testing), so my code looks like this:
print($now = format_date(time(), "custom", "Ymd"));
print(' ');
print( substr($now, 0, 4)); //year
print(' ');
print(substr($now, 4, 6)); //month
print(' ');
print(substr($now, 6, 8)); //day
I'm executing this on the Execute PHP Code within Devel module on a drupal website. This is what i get:
20101029 2010 1029 29
So format_date i开发者_如何学Cs working, year and day are alright, but what about the month? why does it print 4 chars, and not 2 as specified?
PHP's substr is string substr ( string $string , int $start [, int $length ] )
So your code should read as:
print(substr($now, 0, 4)); //year
print(' ');
print(substr($now, 4, 2)); //month
print(' ');
print(substr($now, 6, 2)); //day
http://php.net/manual/en/function.substr.php
substr
accepts $string, $start, and an optional $length. Try using 2 instead of 6 in both month and day.
精彩评论