473,749 Members | 2,350 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jul 17 '05 #1
1 2746
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_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

Jul 17 '05 #2

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

Similar topics

8
3343
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 should be used in a form so when opening the page, automatic all the
3
1649
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 April 3rd? I'm using PHP 4.3.5 Yannick
5
1728
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 (such as June 31, an invalid date) and have the function turn it into a real date by subtraction. In other words, the function would turn the date to June 30, run itself again, and if June 30 is a real date, it'd return it back to the program. ...
3
5934
by: Matt | last post by:
Given a date, how to find the beginning date and ending date of that week please advise!
9
2249
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 at http://webfx.eae.net but it does not seem to be free. Thanks!
5
1539
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); If $y = 2006, $m = 12, and $d = 28, I get a negative value for $reqDateTS. What is wrong with the above statement?
3
2353
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 ("d")+7, date("Y"))); else if ($close_date == 1)
1
2620
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 >= current date. Otherwise, user have to waste time to page up the pages to find the current date 2. I got a script of simple calendar from the web that use mktime() to create links on the calendar Task I need to let user view data earlier than...
10
3281
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"), date("Y")));?> AS
0
8996
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8832
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9566
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6800
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.