2

I want to concatenate 3 strings. For first string I am splitting it to get first 4 letters. Another is user_name and the last is date_of_application.

we use .(dot operator) in PHP. But as I am doing in this, In result first two strings are concatenated well but third i.e. date_of_application, only first 2 or 4 letters are getting concatenated not the whole string.

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

Input:

28/08/2016,28082016

results:

Indisid128/0, Indisiddhi28

EDIT:

I want to get the digits from date.Input date can be in any format. For example- if input date is 28 Aug 2016. The output should be concatenated string _ 28082016. How can I get this?

What's going wrong?

Sid
  • 2,792
  • 9
  • 55
  • 111

3 Answers3

3

You can not concatenate date with string with your existing format. You need to convert it (slashes are removed).

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

Write your code as below:

$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;
MarthyM
  • 1,839
  • 2
  • 21
  • 23
Ravi Hirani
  • 6,511
  • 1
  • 27
  • 42
  • I am getting this message when I run the code. Its getting concatenated well though. It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. @RaviHirani – Sid Aug 29 '16 at 11:48
  • @Sid Please check this link:- http://stackoverflow.com/questions/16765158/date-it-is-not-safe-to-rely-on-the-systems-timezone-settings – Ravi Hirani Aug 29 '16 at 11:50
  • What if input date is in different format? like 28-08-2016 or 28 Aug 2016? @RaviHirani – Sid Aug 29 '16 at 11:50
  • it's showing this format - 1970_01_01-00-00-00 , I only want the digits from date , I want to save date in 8 digits. Like if input is 28 aug 2016 the output should be concatenated string + 28082016. @RaviHirani – Sid Aug 29 '16 at 12:11
  • I tried changing the timezone but it dose not give date as desired. input is- 28/08/2016 , output is - 1970_01_01-04-00-00 . @RaviHirani – Sid Aug 29 '16 at 12:45
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;

I try this and it's working fine

Vinod Kumar
  • 352
  • 3
  • 7
  • It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. @Vinod Kumar – Sid Aug 29 '16 at 11:48
  • What if input date is in different format? like 28-08-2016 or 28 Aug 2016? @Vinod Kumar – Sid Aug 29 '16 at 11:50
  • if format is 28-08-2012 than it will give the result "IndiTestUser08-29-2016" @ Sid Where are you using this.. in any framework ? – Vinod Kumar Aug 29 '16 at 11:55
  • I only want to save the 8 digits of date. @Vinod Kumar – Sid Aug 29 '16 at 11:57
-2

Consider casting the value of $this->date_of_application to string, like:

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

Alternatively, in scenarios where you want to be using variable values within strings, you could use interpolation instead of concatenation, like:

$this->member_id = mb_substr($this->country, 0, 4) . "{$this->user_name}{$this->date_of_application}";
  • 2
    The value will automatically be cast to a string when used with the *string concatenation operator*… – deceze Aug 29 '16 at 12:29
  • Any reference you can provide for that? – paulatumwine Aug 29 '16 at 12:49
  • 1
    [The manual](http://php.net/manual/en/language.operators.string.php) doesn't seem to make any explicit mention of it, but it's easy to test experimentally: `"" . []` → "Array", which is the expected result of an array-to-string cast. – deceze Aug 29 '16 at 12:55
  • I guess you're right. I've always used casting in function calls to ensure that parameters passed to the function are of the same types that the function argument lists expect. I realize this is superfluous for string concatenation. – paulatumwine Aug 29 '16 at 13:12