substr() 没有按预期工作
substr() is not working as expected
我只是想分别提取日期的 year
、month
和 day
,以便我可以按我的意愿使用它。
我将当前日期存储在 $today
中并使用 substr()
从中提取字符串。但是我在做的事情中出现了一些奇怪的行为。
我当前的代码:
$today = date("Y/m/d");
$_year = substr($today, 0,4);
$_month = substr($today, 5,7);
$_day = substr($today, 8, 10);
echo $_year . " " . $_month;
$_year
按预期正常工作,但问题出在 $_month
,无论我从哪个位置开始我的 substr()
月份和日期都会相互配对。
谁能帮我解决这个问题?这让我发疯。
这应该适合你:
只需explode()
your date by a slash and then use a list()
分配变量。
list($year, $month, $day) = explode("/", $today);
只需使用:
echo date("Y m");
如果您想将日期的每个部分都放在一个单独的变量中,我强烈建议您使用 DateTime
class:
$dt = new DateTime();
$year = $dt->format('Y');
$month = $dt->format('m');
$day = $dt->format('d');
echo $dt->format('Y m');
你应该看看 substr
参考:http://php.net/manual/it/function.substr.php
该函数接受 3 个参数:$length
是您要从 $start
开始剪切的字符串的长度
string substr ( string $string , int $start [, int $length ] )
对于您的情况,这将正常工作:
$today = date("Y/m/d");
$_year = substr($today, 0,4);
$_month = substr($today, 5,2);
$_day = substr($today, 8, 2);
echo $_year." ".$_month;
$today = date("Y/m/d");
$_year = substr($today, 0,4);
$_month = substr($today, 5,7);
$_day = substr($today, 8, 10);
echo $_year." ".$_month;
应该是
$today = date("Y/m/d");
$_year = substr($today, 0,4);
$_month = substr($today, 5,2);
$_day = substr($today, 8, 2);
echo $_year." ".$_month;
substr()
并不如你所想
您在想:
string substr ( string $string , int $start , int $end )
但它是:
string substr ( string $string , int $start , [int $length)
所以使用 substr($today,5,2)
表示月份,substr($today,7,2)
表示日期
您可以将其用作
$today = date("Y/m/d");
$today = explode("/", $today);
$year = $today[0];
$month = $today[1];
$day = $today[2];
echo $year . ' ' . $month .' '. $day; // 2015 05 05
我只是想分别提取日期的 year
、month
和 day
,以便我可以按我的意愿使用它。
我将当前日期存储在 $today
中并使用 substr()
从中提取字符串。但是我在做的事情中出现了一些奇怪的行为。
我当前的代码:
$today = date("Y/m/d");
$_year = substr($today, 0,4);
$_month = substr($today, 5,7);
$_day = substr($today, 8, 10);
echo $_year . " " . $_month;
$_year
按预期正常工作,但问题出在 $_month
,无论我从哪个位置开始我的 substr()
月份和日期都会相互配对。
谁能帮我解决这个问题?这让我发疯。
这应该适合你:
只需explode()
your date by a slash and then use a list()
分配变量。
list($year, $month, $day) = explode("/", $today);
只需使用:
echo date("Y m");
如果您想将日期的每个部分都放在一个单独的变量中,我强烈建议您使用 DateTime
class:
$dt = new DateTime();
$year = $dt->format('Y');
$month = $dt->format('m');
$day = $dt->format('d');
echo $dt->format('Y m');
你应该看看 substr
参考:http://php.net/manual/it/function.substr.php
该函数接受 3 个参数:$length
是您要从 $start
string substr ( string $string , int $start [, int $length ] )
对于您的情况,这将正常工作:
$today = date("Y/m/d");
$_year = substr($today, 0,4);
$_month = substr($today, 5,2);
$_day = substr($today, 8, 2);
echo $_year." ".$_month;
$today = date("Y/m/d");
$_year = substr($today, 0,4);
$_month = substr($today, 5,7);
$_day = substr($today, 8, 10);
echo $_year." ".$_month;
应该是
$today = date("Y/m/d");
$_year = substr($today, 0,4);
$_month = substr($today, 5,2);
$_day = substr($today, 8, 2);
echo $_year." ".$_month;
substr()
并不如你所想
您在想:
string substr ( string $string , int $start , int $end )
但它是:
string substr ( string $string , int $start , [int $length)
所以使用 substr($today,5,2)
表示月份,substr($today,7,2)
表示日期
您可以将其用作
$today = date("Y/m/d");
$today = explode("/", $today);
$year = $today[0];
$month = $today[1];
$day = $today[2];
echo $year . ' ' . $month .' '. $day; // 2015 05 05