I get the string in the form "1990-03-02" .

This, as you can see, is a date. How to divide this date into 3 parts so that it is:

 $a = 1990; $b = 3; $c = 2; 
  • 2
    explode or strtotime and after date - Naumov

4 answers 4

Once you are working with a date, you can use specialized PHP tools to work with date / time, namely the class \DateTime . And you can do it like this:

 $d = DateTime::createFromFormat('Ym-d', '1990-03-02'); $a = (int)$d->format('Y'); $b = (int)$d->format('j'); $c = (int)$d->format('n'); 

Working example on IDEOne.

Note :

For this particular task with splitting a string into three variables, the approach using \DateTime is somewhat redundant. However, it gives you, on the one hand, an incredible flexibility of formatting the result, and on the other, it allows you to think about the date as a date, and not as a set of three variables. All this, ultimately, allows you to increase the level of code abstraction and reduce its total complexity.

And yet, with this approach, you can easily get other parameters of your original date with zero labor costs. So, for example, you can get the number of the week:

 $week = (int)$d->format('W'); 

Using a special data type for a date allows you to do more cool things using a clear high-level language. For example, you can add to your date an arbitrary interval , say, a week:

 $new_date = $d->add(new DateInterval('P1W')); 
  • Show how easy it is to add a week to this date - it will be useful to many, I think. - Sergiks
  • @Sergiks, you're right. updated the answer - Dmitriy Simushev
  • I just about the addition / subtraction of the interval suggested, because (crystal ball) maybe, the task of the TS is to get something similar, and it is going to add to days 7. And you can $d->add(new DateInterval('P1W')); - Sergiks 9:39 pm
  • one
    @Sergiks, updated again =) - Dmitriy Simushev

You can use the sscanf() function to parse a formatted string:

 $dateString = "1990-03-02"; list($a, $b, $c) = sscanf($dateString, "%d-%d-%d"); 

    $ a = explode ('-', $ str); so get a split array. Well, at zero in front check every element of the array is not a problem ...

     $str = "1990-03-02"; $a = explode('-', $str); $year = $a[0]; if($a[1][0]==0){$month = $a[1][1];}else{$month = $a[1];} if($a[2][0]==0){$day = $a[2][1];}else{$day = $a[2];} 

    either ternary check

     $str = "1990-03-02"; $a = explode('-', $str); $year = $a[0]; $a[1][0]==0 ? $month = $a[1][1] : $month = $a[1]; $a[2][0]==0 ? $day = $a[2][1] : $day = $a[2]; 
    • Why doesn't a person have a complete answer? - Dmitriy Gvozd
    • @ Dmitry Nail, the answer is quite complete, I don’t think it’s necessary to give as soon as possible the most complete solution - the person will stop trying to figure it out himself, there’s no place for concrete decisions, it’s better for mine to just push the person to the right solution, but whatever you like, he added Boris
    • @Sergiks, you are right, I didn’t foresee ... thank you, I’ll fix it now - Boris Runs
    • Do you know this syntax условие ? если_да : если_нет условие ? если_да : если_нет ? For example, $month = $a[1][0]==='0' ? $a[1][1] : $a[1]; $month = $a[1][0]==='0' ? $a[1][1] : $a[1]; - Sergiks
    • Is that allowed? Interestingly, it will be necessary to look at this option. I have not seen this ... - Boris Runs
     list($a, $b, $c) = explode('-', '1990-03-02'); 
    • Are you sure about the code? php.su/explode - Dmitry Gvozd
    • I agree with Dmitry on this and minus canceled the separator in the exporter, specify only - Naumov
    • Corrected. Although this is not the best option. Better use a regular expression, so you can check the format - Ihor Ivanovich
    • do you really think that topstarter will read the last of your comments here? He should be responsible and not here. - Dmitry Gvozd
    • 2
      Why check the format? No need to complicate the task of the vehicle. And then we’ll check that the month is not the 26th, but the number is not the 59th:) - Sergiks