473,789 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Time/Date conversion year/week -> day/month/year

Jezternz
145 New Member
Okay. Simple as it sounds, but possibly complex.

I have $week (which is the week in a year 1-52) and I have $year.
I want to get the day of the saturday in the given $week and $year, so i want returned:
$day (being day of month, will be the saturday of he origonal given week), $month (the month 0-12) and $year.

Bit of a brain tease for me atleast. Any Idea's?
May 2 '08 #1
21 3188
TheServant
1,168 Recognized Expert Top Contributor
Look under Calendar Calculating for a mathematical way of doing this.

This one might be more useful ina code sense, but they have the same logic.

I would really appreciate you sharing this code, as it is an interesting one to do! That second one looks like it will give more, but you might combine them for a solution.
May 2 '08 #2
Jezternz
145 New Member
I would really appreciate you sharing this code, as it is an interesting one to do! That second one looks like it will give more, but you might combine them for a solution.
sure thing, I will post with the working code, when I get it :)
May 2 '08 #3
TheServant
1,168 Recognized Expert Top Contributor
sure thing, I will post with the working code, when I get it :)
Very interesting page (the second link). The doomsday code is amazing, and even possible to do in your head if your practice! Even if you get stuck, post the code, and maybe we can help then.
May 2 '08 #4
Jezternz
145 New Member
im not sure if I explained what im trying to do properly, so in the chance I did not I will give an example.

Input:
we are in 2008, obviously.
$year = 2008
It is the 18th week of the year (this can be found by date('W');
$week = 18

now what I want for output is the day/month/year of the saturday of in this case week 18 so.
output:
again obviously 2008
$year = 2008
the saturday in the 18th week, is in May, however do not forget we want the date for the saturday, not the date for the 18th week, so although a week may start in one mont, it may be the next month im looking for,
$month = 05
The day of the saturday.
$day = 3

Thanks :)
May 2 '08 #5
TheServant
1,168 Recognized Expert Top Contributor
No one is going to make you this code. We fix your code, not make it. Because it is days of the week, and this changes from year to year, I gave you those links so that you could start developing a day finder code yourself. If it's not working, or you don't know how to do a part of it, that's when you post here.
But you have to provide the start.
I did understand what you wanted, but again, the coding involved is more than a few lines, so the unpaid coders (everyone) here will be unlikely to make it for you.
May 2 '08 #6
Jezternz
145 New Member
ok will do, thanks again, ill report back when I got something.

Cheerz, Josh
May 2 '08 #7
coolsti
310 Contributor
I have done such similar manipulations for my application, and out of curiosity was trying to look for a code snippet that can solve this problem.

But I am stuck with how to find out in PHP the month number when given the week number and year! Of course you can go the other way around, and find the week number for a given month and year using the date() function.

Once you can find out how to get month from week number and year, the rest of what the OP is asking for is trivial, using some basic PHP functions.

Does anyone know how to do this? Get the month from the week number plus year?
May 2 '08 #8
Jezternz
145 New Member
Yeh I know aye, Im pretty stumpted tbh, im sure it must be possible, I just dont know where to start and yeh I did wonder about it being easier to go back the other way.
May 3 '08 #9
Jezternz
145 New Member
Ok I got this:

Expand|Select|Wrap|Line Numbers
  1. // Define day + week in secs
  2. $one_week_secs = 60*60*24*7;
  3. $one_day_secs = 60*60*24;    
  4.  
  5. // Get week
  6. $week = date('W'); // use current week number for an example
  7.  
  8. // get upto start of year time
  9. $upto_this_year_secs = mktime (0, 0, 0, 1, 0, date('Y'));
  10.  
  11. // Get days till sat
  12. $day_of_week = getdate($upto_this_year_secs);
  13. $days_till_sat = 6-$day_of_week['wday'];
  14.  
  15. $timestamp = $upto_this_year_secs+($week*$one_week_secs)+($days_till_sat*$one_day_secs);
  16.  
  17. $week_current_saturday = getdate($timestamp);
  18.  
  19. echo "day:".$week_current_saturday['mday']." month:".$week_current_saturday['mon']." year:".$week_current_saturday['year']."<br/>";
  20.  
Ok almost there :)
Only problem is, its saying this week's saturday is the 9th when it is actually the 10th. Now if I change the bolded/underlined 6 to '7' then the dates are corrected, however this doesnt make sence to me? and im not sure if I should change it as, although it works this year it might not work next year. I dont understand why 7 would work, you will have to think through the process, but because saturday is '6' (and sunday is '0') I dont get why it needs to be 7? any ideas?
May 4 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
11723
by: dan glenn | last post by:
Say, I want to set a cookie and have it expire an hour after it's set. It's looking like this is only possible for browsers which are in the same time zone as my server?? In other words, if I set my cookie with: setcookie('CookieName', $SomeValue, time()+3600, "/");
3
17916
by: CrystalDBA | last post by:
I am using SQL Server 2000. I need to query my database for all the contracts that came in during a certain time frame (user is prompted for reportingperiodid). Table - Periods Fields - Reporting Period id int Reporting Period desc varchar(30) Reporting Period Begin Date datetime Reporting Period End Date datetime
1
11688
by: heirou | last post by:
I'm a novice in this subject....I've made a database that requires a time conversion. For example, if local time is 1200, determine the time in Korea. I use two fields: a date field, and a time field. I need the converted time to show up in a report. I also need the corresponding date to increment if necessary. Any ideas on how to do this would be greatly appreciated.
3
29040
by: jerry.ranch | last post by:
I have a need to convert simple dates (i.e. 02/14/2005) to a number, do some math, and convert back to a date. (in a simple query). The math involves adding or substracting days, and days of the week . I've used the weekday() function to convert dates to numberic days of the week (1-7) I've used cdbl (date) to convert a date to a serial number, but then I do math with the number and I can't seem to convert this back to a date.
3
7458
by: gregmalenky | last post by:
Visual C# 2005 Express - I am creating a employee scheduling program for work. When I need to do is for the program to open with a start date of the previous sunday. I also want the ability to change the start date to create a new schedule - however, it must begin on a sunday. I want the sunday date to be locked in so it doesn't always show the current date when executed. Also for a prompt to appear if the user tries to use another day...
4
2686
by: richardkreidl | last post by:
How would I check to see if the current time(military format) is greater then 07:30AM and the day of the week is Monday-Friday. Pseudo code: If Current_Time > 07:30AM and Current_Day = Monday or Current_Day = Tuesday _ or Current_Day = Wednesday or Current_Day = Thursday or Current_Day = Friday then
6
3310
by: fniles | last post by:
I am using VB.NET 2003 and SQL Server 2000. I have a table with a datetime column type. When inserting into the table for that column, I set it to Date.Now.ToString("T") , which is something like "2:50:54 PM". But after the row is inserted and I check the data in the database, the column data is set to "1/7/2007 2:50:04 PM" (notice today's date in front of the time). If I insert data directly into the table in the Enterprise Manager, the...
6
5161
by: dredge | last post by:
Hi, the server that hosts my PHP pages has its clock set to Greenwich Mean Time (GMT timezone 0). I need for my PHP scripts to have access to my local time which is Central Standard Time in the U.S. (CST timezone -6). Note: daylight savings time _is_ observed in my state. I have looked all over a PHP algorithm that would convert GMT to CST but have so far not been successful. Does anyone have such an algorithm lying around that they could...
2
1757
by: nex85 | last post by:
hi! HOUR FROM TIME i) does anyone know how to determine which hour a time value lies in? the arrival time is in hh:mm:ss format. for e.g.: for an arrival time of 17:00:26, the correct conversion should be: 17:00. DAY OF THE WEEK FROM DATE IN VBA ii) if a cell's value is a date, in the form: 14/08/2006, how would you detect which day of the week is it? like monday, tuesday, wednesday, etc. APPEND DATE AND DATE STRING iii) is it...
1
2017
by: assgar | last post by:
Hi I need help. I know what I want to accomplish, but I do not know how to do it. WHAT I NEED HELP ACCOMPLISHING: How to do I insert data into a table for a date range of two or more months, where every second or third week should be able to have different events/appointments.
0
9666
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
10408
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
9983
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
7529
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
6769
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
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4092
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3700
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
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.