Log in

View Full Version : help with converting string ..



mulaus
11-06-2012, 04:49 AM
Hi im a php newbie

i have this code


<?
$noic = "811009-xx-xxxx";
$ex = explode('-',$noic);
$sp = str_split($ex[0]);

$year = $sp[0].$sp[1];
$month = $sp[2].$sp[3];
$day = $sp[4].$sp[5];

echo $day.'-'.$month.'-'.$year;

?>


it will change noic = 811009-xx-xxxx to date 09-10-81

what if my noic is in this format 811009xxxxxx

how do i do this..

TQ

jscheuer1
11-06-2012, 06:50 AM
<?php
$noic = "811009-xx-xxxx";
$sp = substr($noic, 0, 6);

$year = $sp[0].$sp[1];
$month = $sp[2].$sp[3];
$day = $sp[4].$sp[5];

echo $day.'-'.$month.'-'.$year;
?>

See:

http://www.php.net/manual/en/function.substr.php

mulaus
11-06-2012, 07:07 AM
thank you bro..

youre the man..

i love php :-)

traq
11-06-2012, 02:11 PM
Note that if you use a more standard date format, you can use the DateTime (http://PHP.net/date time) class, which is designed for exactly this task.