12

How can I get the last 2 digits of:

<departureDate>200912</departureDate>

to my object:

$year = $flightDates->departureDate->year;
3
  • is it $flightDates->departureDate->year that returns <departureDate>200912</departureDate>
    – Baba
    Sep 28, 2012 at 12:06
  • Dont think so. Looks like the number is extracted from XML
    – Peon
    Sep 28, 2012 at 12:09
  • Take a look into the date() function and the strtotime() function at php.net.
    – Jan.
    Sep 28, 2012 at 12:14

6 Answers 6

29
// 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:

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

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

<?php

//$year = '200912';
$year = $flightDates->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:

<?php

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

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

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

?>
0
4

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

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

echo $year % 100;
?>
2

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

0
1

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

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

Example 2 : You can also use simplexml_load_string with substr

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

Output

string '20' (length=2)
0

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

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.

4
  • In java you can do this by following code. Let the variable is an integer named x . then you can use byte[] array= Integer.toString(x).get.bytes(). Now array[0] and array[1] is the first two digits. Sep 28, 2012 at 12:15
  • Edit your answer and add this code there, don't forget to format it too.
    – Yaroslav
    Sep 28, 2012 at 12:23
  • 1
    Why are you telling how to do it in Java if the OP specified that its for PHP ?
    – Peon
    Sep 28, 2012 at 12:29
  • I think it's not a matter whether you use java or php. The idea is the main thing. you can find the php equivalent functions very easily on the web. That's why !!! Sep 28, 2012 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.