Page 1 of 2
PHP date() functions
Posted: Fri Jul 07, 2006 10:53 pm
by tristanlee85
I was looking at the date() function on php.net because what I am wanting to do is display the week summary and have it update every week. For example, since today is July 7th, 2006, I'd like to echo "Week of 6/3/06 to 6/7/06" using only the Monday through Friday format, but weekends can be acceptable. I found a function such as this:
Code: Select all
function findWeekPeriod($yearweek){
$aPeriod = array();
$year = substr($yearweek,0,4);
$week = substr($yearweek,4,2);
$startDay = Mon;
$endDay = Sun;
$startdate = strtotime('+' . $week . ' week',mktime(0,0,0,1,1,$year));
$enddate = $startdate;
while(date("D",$startdate) != $startDay){
$startdate = mktime(0,0,0,date("m",$startdate),date("d",$startdate)-1, date("Y",$startdate));
}
while(date("D",$enddate) != $endDay){
$enddate = mktime(0,0,0,date("m",$enddate),date("d",$enddate)+1, date("Y",$enddate));
}
return array('start' => date('l d/m/y', $startdate),
'end' => date('l d/m/y', $enddate));
return $aPeriod;
}
but I'm really unsure how to use it or if it's even what I want. Can anyone help?
Posted: Sat Jul 08, 2006 5:23 am
by Oren
First of all, when you said: "Week of 6/3/06 to 6/7/06", you actually meant: "Week of 7/3/06 to 7/7/06".
July is the 7th month of the year.
Now to your question... What are you trying to do? Why do you need $aPeriod? What are you going to do with it?
I ask all these questions since there might be another, perhaps better, way to do what you are trying to do.
Posted: Sat Jul 08, 2006 1:53 pm
by tristanlee85
My apologies. That isn't my function. I just found it as an example on
http://www.php.net. What I'm trying to do is develop a misload report for my team at FedEx. This is the basic layout, but it's obviously going to have more functions as I continue to work on the script.
http://teams.fedexunderground.com Each day I will be posting how many packages were misloaded and they will be able to view the site and see how well their team is doing. So, our goal is the team with 0 misloads for the week will receive a prize. At the top of the table, I'd like to be able display which week it is giving the day range of Monday through Friday because I will eventually create a drop-down menu for them to choose from to either go back and see how they did in previous week. I'm just trying to find a way to do this without manually typing it in everytime I update the page.
Ignore the "6" month. I'm a little behind.
Posted: Sat Jul 08, 2006 2:37 pm
by Oren
Do you mean something like this?
Mon Jul 03 - Fri Jul 07
Posted: Sat Jul 08, 2006 3:40 pm
by Skara
Code: Select all
function getweekperiod() {
$beg = time();
$end = time();
$today = date('D');
switch ($today) {
case 'Sun':
$end += (6 * 86400);
case 'Mon':
$beg += (1 * 86400);
$end += (5 * 86400);
case 'Tue':
$beg += (2 * 86400);
$end += (4 * 86400);
case 'Wed':
$beg += (3 * 86400);
$end += (3 * 86400);
case 'Thu':
$beg += (4 * 86400);
$end += (2 * 86400);
case 'Fri':
$beg += (5 * 86400);
$end += (1 * 86400);
case 'Sat':
$beg += (6 * 86400);
}
return 'Week of '.date('n/j/y',$beg).' to '.date('n/j/y',$end);
}
86400 = 24 * 60 * 60 = number of seconds in a day.
Untested. It could probably be written better...
Edit: If you want to be able to input any day and get the week for that day...
Code: Select all
function getweekperiod($day) { // $day = '6/8/06'
//and
$today = date('D',strtotime($day));
Posted: Sat Jul 08, 2006 4:48 pm
by RobertGonzalez
Are you going to be fetching information from a database? If so, why can't you use the dates in the query where clause as your start and end dates in your header? It seems to me that you are doing more work than you need to to get the ranges.
Posted: Sat Jul 08, 2006 6:51 pm
by Skara
Are you talking to me or him? I don't quite understand your question in any case. The way I understood tristanlee's question was that he wanted to figure out a week range from a singular date.
Posted: Sat Jul 08, 2006 10:45 pm
by RobertGonzalez
Sory, was talking to tristanlee. The reason I ask is that he may be pulling information from a database (the misrouted items or whatever it was) and those records are going to be selected based on a range of dates. If he knows those dates to use in the query, then he can just use those dates in his page header. It was just a suggestion. If it works, cool. If it doesn't, just as cool.
Posted: Sat Jul 08, 2006 10:49 pm
by tristanlee85
Let's say I created a report for this week. At the top of the report I would like it to say:
Week of 7/3/06 to 7/7/06
And then next Monday the top of the report will say:
Week of 7/10/06 to 7/14/06
Here is exactly what I want except this is a simple drop-down menu with me putting the dates in manually. I'm basically wanting a function that can do this automatically. Take a look at the drop-down menu. As the weeks advance, I would like the value of the new week to add itself to the menu. I'm assuming I will need to use a database for this, and if so that is fine as I'm suing a databse to store all of the team information.
http://teams.fedexunderground.com/
Posted: Sun Jul 09, 2006 2:50 am
by Oren
It sounds to me that you haven't thought enough about how you are going to code this thing. Please take your time to think and plan how your going to do all the things you want, what exactly you want it to have, how you are going to store the information and so on... Then come here and ask a more specific question - we can't help you if you don't even know whether you are going to use a database or not.
P.S Your site doesn't work for me.
Posted: Sun Jul 09, 2006 7:07 pm
by RobertGonzalez
Forgot to tell you about that, too, tristanlee. I couldn't load the page either. Server timed out.
Posted: Tue Jul 11, 2006 9:08 pm
by tristanlee85
Sorry. I had it working on my local server only. Anyway, I have a great idea of what I want to do. In fact, I know how to do everything entirely except the date. I've created a number of sites using php and mySQL. Basically I know how to do everything except for this date thing.
Posted: Wed Jul 12, 2006 10:04 am
by pickle
1) Get the timestamp for today
2) Find out what day of the week it is
3) Subtract however many days you need to get to the last Monday - Bam you've got your first stamp
4) Add 5 (or 7) days to that stamp to get your last stamp
strtotime() will be helpful here
Posted: Wed Aug 09, 2006 11:41 pm
by tristanlee85
Bringing back this post. I'm using strtotime() to store a timestamp in the database. How do I convert the timestamp to something readable like 08/10/06?
Posted: Wed Aug 09, 2006 11:47 pm
by RobertGonzalez
In MySQL,
FROM_UNIXTIME().
In PHP,
date().