2016-08-29 59 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