php 返回最后 2 位数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12639642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:56:55  来源:igfitidea点击:

Return the last 2 digits a number

php

提问by UI Developer

How can I get the last 2 digits of:

我怎样才能得到最后两位数字:

<departureDate>200912</departureDate>

to my object:

到我的对象:

$year = $flightDates->departureDate->year;

回答by Jan.

// first two
$year = substr($flightDates->departureDate->year, 0, 2);
// last two
$year = substr($flightDates->departureDate->year, -2);

But given the fact that you're parsing a date here it would be smarter to use the date function. p.e. strtotime()and date()or even:

但是鉴于您在这里解析日期这一事实,使用 date 函数会更明智。PEstrtotime()date()甚至是:

<?php
$someDate ='200912';
$dateObj = DateTime::createFromFormat('dmy', $someDate);
echo $dateObj->format('Y');
// prints "2012" .. (see date formats)

回答by Peon

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

您可以substr将其作为字符串寻址,该函数将自动转换它:

<?php

//$year = '200912';
$year = $flightDates->departureDate->year;

echo substr( $year, -2 );

?>

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

仔细看看substr函数。如果你希望结果是一个严格的整数,那么只需(int)在 return 前面添加。

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

但是,正如Jan.所说,你应该更好地将它作为约会对象:

<?php

//$year = '200912';
$year = $flightDates->departureDate->year;

$date = DateTime::createFromFormat( 'dmy', $year );

echo date( "y", $date->getTimestamp() );

?>

回答by Herbert Van-Vliet

For numbers, you could use php's modulo function (http://php.net/manual/en/internals2.opcodes.mod.php):

对于数字,您可以使用 php 的模函数(http://php.net/manual/en/internals2.opcodes.mod.php):

<?php
//$year = '200912';
$year = $flightDates->departureDate->year;

echo $year % 100;
?>

回答by ARibas

You could use a basic aritimethic operation, like this...

你可以使用一个基本的算术运算,就像这样......

while ( $var > 100 ) 
{
    $var = (int) $var / 10;
}

There could be a better solution but this one will fit fine

可能有更好的解决方案,但这个解决方案很合适

回答by Baba

Example 1 : You can just strip the tags with strip_tagsand use substr

示例 1 :您可以将标签剥离strip_tags并使用substr

$number = '<departureDate>200912</departureDate>' ;
$year = substr(strip_tags($number), 0,2);
var_dump($year);

Example 2 : You can also use simplexml_load_stringwith substr

示例 2:您还可以使用simplexml_load_stringwithsubstr

$number = '<departureDate>200912</departureDate>' ;
$year = substr(simplexml_load_string($number),0,2);
var_dump($year);

Output

输出

string '20' (length=2)

回答by Debobroto Das

convert it to string. then take the first two element.

将其转换为字符串。然后取前两个元素。

In java you can do this by following code. Let the variable is an integer named x . then you can use

在 Java 中,您可以通过以下代码执行此操作。让变量是一个名为 x 的整数。然后你可以使用

byte[] array= Integer.toString(x).get.bytes(). 

Now array[0] and array[1] is the first two digits. array[array.length-1] and array[array.length] are the last two.

现在 array[0] 和 array[1] 是前两位数字。array[array.length-1] 和 array[array.length] 是最后两个。