2016-08-29 26 views
1

我想连接3个字符串。对于第一个字符串,我将它分成前四个字母。另一个是user_name,最后一个是date_of_application如何在PHP中连接多个字符串?

我们在PHP中使用.(点运算符)。但正如我在这做的,结果前两个字符串连接良好,但第三个,即date_of_application,只有前2或4个字母被连接而不是整个字符串。

$this-> member_id = mb_substr($this->country, 0, 4) . $this->user_name . $this->date_of_application; 

输入:

28/08/2016,28082016

结果:

Indisid128/0,Indisiddhi28

编辑:

我想从date.Input日期的数字可以以任何格式。例如 - 如果输入日期是2016年8月28日。输出应连接字符串_ 28082016.我怎样才能得到这个?

怎么回事?

+2

两个结果似乎有12个字符的长度。是否有变量'member_id'限于12个字符? – mnille

+0

是的这是正确的限制是12. @mnille – Sid

+0

Waht是'$ this-> date_of_application'? –

回答

2

您不能将datestring与您现有的格式连接起来。您需要将其转换(删除斜杠)。

date('Y_m_d-H-i-s', strtotime($this->date_of_application));

如下编写代码: -

$country = mb_substr($this->country, 0, 4); 
$username = $this->user_name; 
$converted_date = date('Y_m_d-H-i-s', strtotime($this->date_of_application)); 
$this-> member_id = $country.$username.$converted_date; 
+0

我在运行代码时收到此消息。尽管如此,它可以很好地连接起来。依靠系统的时区设置是不安全的。您*必须*使用date.timezone设置或date_default_timezone_set()函数。如果您使用这些方法中的任何一种,并且仍然收到此警告,则很可能是拼写错误的时区标识符。我们现在选择了“UTC”时区,但请设置date.timezone以选择您的时区。 @RaviHirani – Sid

+0

@Sid请检查此链接: - http:// stackoverflow。com/questions/16765158/date-it-is-not-safe-to-the-the-systems-timezone-settings –

+0

如果输入日期格式不同,该怎么办?像28-08-2016或2016年8月28日? @RaviHirani – Sid

0
$date_of_application = date('m/d/Y'); 
$user_name = 'TestUser'; 
$country = 'India'; 
echo $member_id = mb_substr($country, 0, 4) . $user_name . $date_of_application; 

我尝试这和它的正常工作

+0

依靠系统的时区设置是不安全的。您*必须*使用date.timezone设置或date_default_timezone_set()函数。如果您使用这些方法中的任何一种,并且仍然收到此警告,则很可能是拼写错误的时区标识符。我们现在选择了“UTC”时区,但请设置date.timezone以选择您的时区。 @Vinod Kumar – Sid

+0

如果输入日期格式不同,该怎么办?像28-08-2016或2016年8月28日? @Vinod库马尔 – Sid

+0

如果格式是28-08-2012比它会给出结果“IndiTestUser08-29-2016”@ Sid 你在哪里使用这..在任何框架? –

-2

考虑强制的$这个 - 值> date_of_application到string ,如:

$this->member_id = mb_substr($this->country, 0, 4) . $this->user_name . (String)$this->date_of_application; 

或者,当你想使用字符串中的变量值情况下,你可以使用插值而不是串联,如:

$this->member_id = mb_substr($this->country, 0, 4) . "{$this->user_name}{$this->date_of_application}"; 
+1

与*字符串连接运算符*一起使用时,该值将自动转换为字符串... – deceze

+0

您可以提供的任何参考? – anka

+0

[手册](http://php.net/manual/en/language.operators.string.php)似乎没有明确提及它,但实验测试很容易:'“”。 []'→“数组”,这是数组到字符串转换的预期结果。 – deceze