# Date Calculation Problem w/ mktime()

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
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(\$today)) % 7;
\$current_sunday = jdtogregorian(\$this_sunday);
\$this_sundays_parts = explode("/",\$current_sunday);
\$cur_sunday = \$this_sundays_parts[2] . "-" . \$this_sundays_parts[0] .
"-" . \$this_sundays_parts[1];

//  GET LAST WEEK'S SUNDAY

\$last_sunday = date("Y-m-d", mktime(0, 0, 0,
date(\$this_sundays_parts[2]), date(\$this_sundays_parts[0]),
date(\$this_sundays_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!

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

Try the following:

\$this_sunday_ts = mktime(0, 0, 0,
\$this_sundays_parts[0],
\$this_sundays_parts[1],
\$this_sundays_parts[2]
);

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

HTH;
JW