date() question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ZMAN

    date() question

    Hello all.

    Is there an easy way to populate this array with only the working days (no
    Sat or Sun) from the last 60 days.

    for($i = 1; $i <= 60; $i++) {

    $pastDates = date("Y-m-d", mktime(0, 0, 0, date("m")-2, date("d")+$i,
    date("Y")));

    $findPastDates = array($pastDate s);

    }

    Thanks in advance


  • Chris Hope

    #2
    Re: date() question

    ZMAN wrote:
    [color=blue]
    > Hello all.
    >
    > Is there an easy way to populate this array with only the working days
    > (no Sat or Sun) from the last 60 days.
    >
    > for($i = 1; $i <= 60; $i++) {
    >
    > $pastDates = date("Y-m-d", mktime(0, 0, 0, date("m")-2, date("d")+$i,
    > date("Y")));
    >
    > $findPastDates = array($pastDate s);
    >
    > }[/color]

    You can always do it like this, which saves all the calls to date() in
    the loop:

    $day = date('d');
    $month = date('m');
    $year = date('Y');
    $dates = array();

    for($i = -59; $i < 1; $i++) {
    $dates[] = date('Y-m-d', mktime(0, 0, 0, $month, $day+$i, $year));
    }

    This gives you an array like so:

    [0] => 2004-11-23
    [1] => 2004-11-24
    [2] => 2004-11-25
    [3] => 2004-11-26
    ....

    Although we're making the day passed to mktime() negative it correctly
    works out the month and year.

    If you wanted to not include sat or sun you could modify it like so:

    for($i = -59; $i < 1; $i++) {
    $timestamp = mktime(0, 0, 0, $month, $day+$i, $year);
    $day_of_week = date('w', $timestamp);
    if($day_of_week != 0 && $day_of_week != 6) {
    $dates[] = date('Y-m-d', $timestamp);
    }
    }

    --
    Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/

    Comment

    • Tim Van Wassenhove

      #3
      Re: date() question

      On 2005-01-21, ZMAN <vze2mf4r@veriz on.net> wrote:[color=blue]
      > Hello all.
      >
      > Is there an easy way to populate this array with only the working days (no
      > Sat or Sun) from the last 60 days.
      >
      > for($i = 1; $i <= 60; $i++) {
      >
      > $pastDates = date("Y-m-d", mktime(0, 0, 0, date("m")-2, date("d")+$i,
      > date("Y")));
      >
      > $findPastDates = array($pastDate s);
      >
      > }[/color]

      What about http://www.php.net/strtotime ?
      With something like strtotime("now -60 day"); the answer is near ;p

      --
      Met vriendelijke groeten,
      Tim Van Wassenhove <http://www.timvw.info>

      Comment

      Working...