Return the last 2 digits a number

后端 未结 6 1038
Happy的楠姐
Happy的楠姐 2021-01-11 15:00

How can I get the last 2 digits of:

200912

to my object:

$year = $flightDates-&         


        
6条回答
  •  一个人的身影
    2021-01-11 15:27

    You can just address it as string, the function substr will convert it automatically:

    departureDate->year;
    
    echo substr( $year, -2 );
    
    ?>
    

    Take a closer look at substr function. If you want the result to be a strict integer, then just add (int) in front of the return.

    But, as Jan. said, you should better work with it as a date:

    departureDate->year;
    
    $date = DateTime::createFromFormat( 'dmy', $year );
    
    echo date( "y", $date->getTimestamp() );
    
    ?>
    

提交回复
热议问题