473,385 Members | 1,873 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,385 software developers and data experts.

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

Jezternz
145 100+
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 3153
TheServant
1,168 Expert 1GB
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 100+
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 Expert 1GB
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 100+
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 Expert 1GB
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 100+
ok will do, thanks again, ill report back when I got something.

Cheerz, Josh
May 2 '08 #7
coolsti
310 100+
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 100+
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 100+
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
Jezternz
145 100+
Okay found the problem after massive amounts of debugging, it was daylight savings, gah, drives me crazy. anyway, to solve this I just added 12 hours of seconds onto the end (12*60*60)

cheerz
May 4 '08 #11
ronverdonk
4,258 Expert 4TB
After some searching I found a VERY old (20 years?) C routine that I once wrote in the stone age. I managed to recode it to somehow acceptable PHP and the result is here.

P.S. It works for any week > 2 and < 54 and any year within the strtotime range. It also uses monday as the start of the week. But with some alteration it will do.[php]<?php
$monthtab=array(31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365);

function showDayAndMonth($dayno) {
global $monthtab;
$date_arr=array();
for ($j=0; $j<12; $j++) {
if ($dayno <= $monthtab[$j]) {
$date_arr['day']=$dayno-$monthtab[$j-1];
$date_arr['mth']=++$j;
return $date_arr;
}
}
}

$year=2008;
$week=18;

$monday=0-(date("N", strtotime("1 January $year"))-2);
if ($year % 4 == 0)
for ($i=1; $i<12; $i++)
$monthtab[$i]++;
if ($week > 1 AND $week < 54) {
$week--; /* -1 voor offset */
$day_fr=($week * 7) + $monday; /* Dagnummer van weekstart */
$day_to=$day_fr+6; /* Dagnummer van weekeind */
$date_fr=showDayAndMonth($day_fr);
$date_to=showDayAndMonth($day_to);
}
else {
die ("Invalid week number");
}
echo "REQUESTED: week $week, year $year";
echo "<br>STARTDATE: ".date("l F j, Y",strtotime($date_fr['day'].'-'.$date_fr['mth'].'-'.$year));
echo "<br>ENDDATE: ".date("l F j, Y",strtotime($date_to['day'].'-'.$date_to['mth'].'-'.$year));
?>[/php]I can explain the code if anyone is interested.

Ronald
May 4 '08 #12
TheServant
1,168 Expert 1GB
Wow, that is really clever. Good job Ron!
May 5 '08 #13
Jezternz
145 100+
Yeh that is impressive Ron, thanks heaps.
One thing I do wonder about though is wht would happen when february has 29 days not 28 :S.
$monthtab=array(31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365);
But regardless, this is far further then I got, thanks heaps.
May 6 '08 #14
TheServant
1,168 Expert 1GB
Maybe just include an if statement with something like:

[PHP]if ($year == "is not a leap year") {
$monthtab=array(31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365);
} elseif ($year == "is a leap year") {
$monthtab=array(31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 336, 366);
}[/PHP]

You just nee to get a statement to define "is not a leap year". So probably something like divide the year by 4, make it a string, and search the string for a "." and if it is found, it is not a leap year. Then you also need to check if it is the 400th year, so if it can be divided by 400, if so it is also not a leap year.
May 6 '08 #15
ronverdonk
4,258 Expert 4TB
NO sir! Leap year handling is in the code already, otherwise it would not have worked for 2008! The leap year handling is done in the following statements in the code[php]if ($year % 4 == 0)
for ($i=1; $i<12; $i++)
$monthtab[$i]++;[/php]
and you don't want to worry about 400-year cycle because the previous one was 1600 and the next one is 2400. And the strtotime routine does not go that far (just until 2032(?).

Ronald
May 6 '08 #16
TheServant
1,168 Expert 1GB
Good point. I forgot the dates don't go back that far! And I didn't realize that it only goes upto 2032?! That's no good. Means that we will have to recode with a new date function :P

Ron, what does the line: if ($year % 4 == 0) mean?
May 7 '08 #17
ronverdonk
4,258 Expert 4TB
Ron, what does the line: if ($year % 4 == 0) mean?
This uses the modulo operator. The modulo operation finds the remainder of division of one number by another.

Given two numbers, $year (the dividend) and 4 (the divisor), a modulo 4 is the remainder, on division of $year by 4. For instance, the expression "2008 mod 4" would evaluate to 0, while "2007 mod 4" would evaluate to 3.

So when the modulo 4 of year is zero, it is a leap year.

Ronald
May 7 '08 #18
TheServant
1,168 Expert 1GB
This uses the modulo operator. The modulo operation finds the remainder of division of one number by another.

Given two numbers, $year (the dividend) and 4 (the divisor), a modulo 4 is the remainder, on division of $year by 4. For instance, the expression "2008 mod 4" would evaluate to 0, while "2007 mod 4" would evaluate to 3.

So when the modulo 4 of year is zero, it is a leap year.

Ronald
So:
[PHP]5 %4 = 0.25
4 %4 = 0
26 %6 = 0.333
30 %6 = 0[/PHP]Just making sure I understand, because this is useful!
May 7 '08 #19
ronverdonk
4,258 Expert 4TB
No sir, the modulus is the remainder of the division, e.g. 4 mod 3 = 1 because when you divide 4 by 3 the remainder is 1. So:
Expand|Select|Wrap|Line Numbers
  1. 5 % 4 = 1
  2. 4 % 4 = 0
  3. 26 % 6 = 2
  4. 30 % 6 = 0
E.g. n mod 2 is often used to 'flip' between 2-entry indices or determine if a number is even or uneven, i.e. 1%2 = 1, 8%2 = 0, 133%2=1, 8490%2=0, etc.

Ronald
May 7 '08 #20
TheServant
1,168 Expert 1GB
I understand. Thanks for that lesson.
May 7 '08 #21
ronverdonk
4,258 Expert 4TB
I understand. Thanks for that lesson.
You are most welcome.

Ronald
May 8 '08 #22

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

Similar topics

4
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...
3
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 -...
1
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...
3
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...
3
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...
4
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...
6
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...
6
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...
2
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...
1
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...

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.