tags:

views:

61

answers:

3
$date ='20101015';

how to convert to $year = 2010,$month = 10, $day =15

thanks

+3  A: 

You can use the PHP substring function substr as:

$year  = substr($date,0,4);  # extract 4 char starting at position 0.
$month = substr($date,4,2);  # extract 2 char starting at position 4.
$day   = substr($date,6);    # extract all char starting at position 6 till end.

If your original string as leading or trailing spaces this would fail, so its better feed substr trimmed input as. So before you call substr you can do:

$date = trim($date);
codaddict
@Downvoter: Care to explain ?
codaddict
Blind down vote is always bad +1 for correct answer
Sarfraz
I know u were wrong at first glance.No worries every thing is fair in love,war and SO!!!
Wazzy
+1  A: 

You can use substring function

http://www.w3schools.com/php/func_string_substr.asp

$year=substr($date,0,4);
$month=substr($date,4,2);
$day=substr($date,6,2);
Wazzy
+2  A: 

You can do it all in one go with

  • sscanf — Parses input from a string according to a format

Example:

list($y, $m, $d) = sscanf('20101015', '%4d%2d%2d');

or

sscanf('20101015', '%4d%2d%2d', $y, $m, $d);
Gordon