Date Calculation Problem w/ mktime()

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

    #1

    Date Calculation Problem w/ mktime()

    Hi all,

    I am having a problem with calculating a date. What I am looking to do
    is get the date of Sunday of this week, then use mktime() to find out
    other days in the past by subtracting days.

    In the code below, today is Saturday, October 16, 2004, this week's
    Sunday is 2004-10-17. I successfully get that.

    But, when I try to subtract 7 days using mktime(), I instead of getting
    2004-10-10, I get 1969-12-31.

    I have looked through php.net/mktime and searched some example via
    Google, but I stuck. Any help would be greatly appreciated.

    CODE BELOW:

    $today = unixtojd();

    // GET THIS WEEK'S SUNDAY
    $this_sunday = $today + (7 - jddayofweek($to day)) % 7;
    $current_sunday = jdtogregorian($ this_sunday);
    $this_sundays_p arts = explode("/",$current_sund ay);
    $cur_sunday = $this_sundays_p arts[2] . "-" . $this_sundays_p arts[0] .
    "-" . $this_sundays_p arts[1];

    // GET LAST WEEK'S SUNDAY

    $last_sunday = date("Y-m-d", mktime(0, 0, 0,
    date($this_sund ays_parts[2]), date($this_sund ays_parts[0]),
    date($this_sund ays_parts[1])-7));

    // PRINT OUT THE DATES
    print "This Sunday is " . $cur_sunday . "<BR>";
    print "Last Sunday was " . $last_sunday . "<BR>";


    Thanks for any help!
    - Adam
  • Janwillem Borleffs

    #2
    Re: Date Calculation Problem w/ mktime()

    adam wrote:[color=blue]
    > But, when I try to subtract 7 days using mktime(), I instead of
    > getting 2004-10-10, I get 1969-12-31.
    >
    > I have looked through php.net/mktime and searched some example via
    > Google, but I stuck. Any help would be greatly appreciated.
    >[/color]

    Try the following:

    $this_sunday_ts = mktime(0, 0, 0,
    $this_sundays_p arts[0],
    $this_sundays_p arts[1],
    $this_sundays_p arts[2]
    );

    $last_sunday_ts = $this_sunday_ts - (86400 * 7);
    $last_sunday = date("Y-m-d", $last_sunday_ts );


    HTH;
    JW



    Comment

    Working...