substr()无法正常工作

问题描述:

我只是想提取日期的以便我可以按自己的意愿使用它。

I am just trying to extract the date's year, month and day separately so that I can use it as per my wish.

我将当前日期存储在今天的 $ 中$ c>,并使用 substr()从中提取字符串。但是我从我的工作中得到了一些奇怪的行为。

I stored the current date in $today and used substr() to extract the strings from it. But I am getting some strange behaviour from what I am doing.

我当前的代码:

$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()

The $_year works correctly as expected but the problem arises from $_month, no matter what position I start my substr() the month and day gets paired up with each other.

有人可以帮我解决这个问题吗?

Can anyone help me solve this issue? It's driving me crazy.

您应该看看 substr 参考: http://php.net/manual/it/function.substr.php

该函数接受3个参数: $ length 是您要剪切的字符串的长度,从 $ start

That function accepts 3 parameters: $length is the length of the string you want to cut starting from $start

string substr ( string $string , int $start [, int $length ] )

在您的情况下,这将正常工作:

In your case, this will work properly:

$today = date("Y/m/d");
$_year = substr($today, 0,4);
$_month = substr($today, 5,2);
$_day = substr($today, 8, 2);
echo $_year." ".$_month;