1

I have an array that represents UNIX time in int type from a table I can't change. Some of the rows are not full UNIX timestamps but are short by a couple of integers. There is a reason this is so in the table but for my script, I need the string to change the non 10 digit rows into "0" and the 10 digit ones into date("Ymd",?) form. Here's an example of the array $qucls:

Array
(
    [0] => 1332594303
    [1] => 1330960502
    [2] => 1331227649
    [3] => 1331305503
    [4] => 1332594303
    [5] => 1331147102
    [6] => 1332680702
    [7] => 1331301902
    [8] => 1331048163
    [9] => 1332248704
    [10] => 1332421503
    [11] => 31536000
    [12] => 1331816703
    [13] => 604800
    [14] => 0
    [15] => 31536000
    [16] => 1332248703
    [17] => 31536000
    [18] => 1361922903
)

This is the script:

$k=0
$l=0
foreach ($qucls as $dt[$k]){
    if (strlen($dt[$k]) < 10)
      $dt[$k++] = '0';
    else {$dt[$k++] = date("Ymd", $dt[$l++]);
 }
}
for ($l=0; $l < $k; $l++){
}

This is the outcome after the loop:

Array
(
    [0] => 20120324
    [1] => 20120305
    [2] => 20120308
    [3] => 20120309
    [4] => 20120324
    [5] => 20120307
    [6] => 20120325
    [7] => 20120309
    [8] => 20120306
    [9] => 20120320
    [10] => 20120322
    [11] => 0
    [12] => 19700101
    [13] => 0
    [14] => 0
    [15] => 0
    [16] => 19700817
    [17] => 0
    [18] => 19700101
)

Notice the date form is formatted properly until it reaches the 1st integer that is strlen < 10. At that point, it changes the less than 10 length integer to "0" which is correct, but the dates after that are goofed up. It continues to change the < 10 digit ones to 0 correctly.

Can someone help me figure out what is wrong in this loop? I'm not quite getting the right outcome with all those 1970 dates after the ELSE kicks in. I'm still new at this.

Thank you.

1 Answer 1

1

Use the below script

<?php
$qucls = array(
    0 => 1332594303,
    1 => 1330960502,
    2 => 1331227649,
    3 => 1331305503,
    4 => 1332594303,
    5 => 1331147102,
    6=> 1332680702,
    7=> 1331301902,
    8=> 1331048163,
    9=> 1332248704,
    10 => 1332421503,
    11 => 31536000,
    12 => 1331816703,
    13 => 604800,
    14 => 0,
    15 => 31536000,
    16 => 1332248703,
    17 => 31536000,
    18 => 1361922903
    );

foreach ($qucls as $key=>$value){
    if (strlen($value)< 10){
        $dt[] = 0;
    }else{
        $dt[] = date("Ymd", $value);
    }
 }
echo "<pre>";
print_r($array);
print_r($dt);
exit;
?>

and you will get the below output

Array
(
    [0] => 20120324
    [1] => 20120305
    [2] => 20120308
    [3] => 20120309
    [4] => 20120324
    [5] => 20120307
    [6] => 20120325
    [7] => 20120309
    [8] => 20120306
    [9] => 20120320
    [10] => 20120322
    [11] => 0
    [12] => 20120315
    [13] => 0
    [14] => 0
    [15] => 0
    [16] => 20120320
    [17] => 0
    [18] => 20130226
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.