Jump to content


Photo

format "date" in an array


  • Please log in to reply
1 reply to this topic

#1 pgsjoe

pgsjoe
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 20 March 2006 - 09:01 PM

I have this...
$m = $_GET['m'];
if ($m == "") {
    $m = mktime(NULL, NULL, NULL, date('m'), 1, date('Y'));
}

$month = date("m", $m);

$r = mysql_query("SELECT id, date FROM `calendar` WHERE date LIKE '%-$month-%'");

while ($line = mysql_fetch_row($r)) {
    $days[$line[1]] = Array('#'.$line[0], 'linked-day');
}
print_r($days);
which gives me this... (the #numbers are the ID numbers which I'm setting up as anchor links).
Array
(
    [2006-03-30] => Array
        (
            [0] => #7
            [1] => linked-day
        )

    [2006-03-16] => Array
        (
            [0] => #10
            [1] => linked-day
        )

    [2006-03-01] => Array
        (
            [0] => #19
            [1] => linked-day
        )

    [2006-03-15] => Array
        (
            [0] => #21
            [1] => linked-day
        )

)
All I need is just the day without leading zeros, like this...
Array
(
    [30] => Array
        (
            [0] => #7
            [1] => linked-day
        )

    [16] => Array
        (
            [0] => #10
            [1] => linked-day
        )

    [1] => Array
        (
            [0] => #19
            [1] => linked-day
        )

    [15] => Array
        (
            [0] => #21
            [1] => linked-day
        )

)
I tried this...
    $days[substr($line[1],8)] = Array('#'.$line[0], 'linked-day');
and got leading zeros on single digit days...
Array
(
    [30] => Array
        (
            [0] => #7
            [1] => linked-day
        )

    [16] => Array
        (
            [0] => #10
            [1] => linked-day
        )

    [01] => Array
        (
            [0] => #19
            [1] => linked-day
        )

    [15] => Array
        (
            [0] => #21
            [1] => linked-day
        )

)
but I need single digit days to return as 1 instead of 01.

I also tried...
$days[date('j',$line[1])] = Array('#'.$line[0], 'linked-day');

but got...
Array
(
    [31] => Array
        (
            [0] => #21
            [1] => linked-day
        )

)
I'm completely out of options and need help!!!

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 20 March 2006 - 09:22 PM

You almost had it here:
<?php  $days[date('j',$line[1])] = Array('#'.$line[0], 'linked-day'); ?>
The date() function takes as it's second parameter an integer containing the number of second since 1-1-1970, so
<?php  $days[date('j',strtotime($line[1]))] = Array('#'.$line[0], 'linked-day'); ?>
should give you what you're looking for.

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users