473,386 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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($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!
- Adam
Jul 17 '05 #1
1 2724
adam wrote:
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.


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

Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Jeroen | last post by:
Hi, I'm searching but not finding :( I need the code that creates automatic the days in a month So like for this month, it creates the days in this format 01-Oct, 02-Oct, .... This...
3
by: Yannick Turgeon | last post by:
Hello all, One of our PHP program bugged recently. I've reduced the problem to this: April 4th! Anybody can tell me if I'm doing something wrong? Why April 4th is not printing but replaced by...
5
by: gsv2com | last post by:
Maybe I'm tired, but I'm having a small problem with a date function I'm writing. Total noobish I know, but this is just going beyond me for some reason... What I want to happen is send a date...
3
by: Matt | last post by:
Given a date, how to find the beginning date and ending date of that week please advise!
9
by: newtophp2000 | last post by:
I need to add the ability for the users to select a date on one of our web pages. The examples I was given by some users were in JavaScript. Is there a free date picker that I can use? I saw one...
5
by: laredotornado | last post by:
Hi, Using PHP 4.4.4 and I'm getting an odd time stamp attempting to make a php date. Here's the code $reqDate = date("Y-m-d", mktime(0, 0, 0, $y, $m, $d)); $reqDateTS = strtotime($reqDate);...
3
by: JJ | last post by:
Here's the code. $link="http://xbox360cheat.org"; $close_date=$_POST; #last content change check if ($close_date == 0) $close_date = date("Y-m-d H:m:s", mktime(12, 0, 0, date("m"), date...
1
osward
by: osward | last post by:
Hi everyone, Background 1. I have a table that consits 400+ rows of data and is growing by day. The table already has paging links at the bottom but I restricted to display rows of data only >=...
10
by: ashore | last post by:
Guys, the line below just returned "Dec 07" as the date for one month back from today. Hardly life-threatening, but any thoughts? <?php print date("M `y", mktime(0, 0, 0, date("m")-1, date("d"),...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.