Jump to content

format "date" in an array


pgsjoe

Recommended Posts

I have this...
[code] $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); [/code]
which gives me this... (the #numbers are the ID numbers which I'm setting up as anchor links).
[code]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
        )

)[/code]
All I need is just the day without leading zeros, like this...
[code]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
        )

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

)[/code]
but [b]I need single digit days to return as [i]1[/i] instead of [i]01[/i][/b].

I also tried...
[code] $days[date('j',$line[1])] = Array('#'.$line[0], 'linked-day');[/code]
but got...
[code]Array
(
    [31] => Array
        (
            [0] => #21
            [1] => linked-day
        )

)[/code]
I'm completely out of options and need help!!!
Link to comment
https://forums.phpfreaks.com/topic/5371-format-date-in-an-array/
Share on other sites

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

Ken
Link to comment
https://forums.phpfreaks.com/topic/5371-format-date-in-an-array/#findComment-19149
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.