473,803 Members | 3,463 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
21 3191
Jezternz
145 New Member
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 Recognized Expert Specialist
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=showDa yAndMonth($day_ fr);
$date_to=showDa yAndMonth($day_ to);
}
else {
die ("Invalid week number");
}
echo "REQUESTED: week $week, year $year";
echo "<br>STARTD ATE: ".date("l F j, Y",strtotime($d ate_fr['day'].'-'.$date_fr['mth'].'-'.$year));
echo "<br>ENDDAT E: ".date("l F j, Y",strtotime($d ate_to['day'].'-'.$date_to['mth'].'-'.$year));
?>[/php]I can explain the code if anyone is interested.

Ronald
May 4 '08 #12
TheServant
1,168 Recognized Expert Top Contributor
Wow, that is really clever. Good job Ron!
May 5 '08 #13
Jezternz
145 New Member
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 Recognized Expert Top Contributor
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 Recognized Expert Specialist
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 Recognized Expert Top Contributor
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 Recognized Expert Specialist
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 Recognized Expert Top Contributor
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 Recognized Expert Specialist
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

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

Similar topics

4
11726
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
11690
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
29044
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
2689
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
3312
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
5165
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
1758
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
2018
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
9703
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
10316
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10295
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
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...
0
9125
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5500
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
5629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
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
3798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.