 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
04-23-2008, 01:55 PM
|
#1 (permalink)
|
|
The Contributor
Join Date: Jan 2008
Location: Leeds
Posts: 52
Thanks: 7
|
date/mktime for next week?
Hi,
I'm trying to work out an easy way to calculate the date for next week based on the current date.
im using
PHP Code:
$varOne= mktime(0, 0, 0, date("m") , date("d"), date("Y"));
$varTwo= date('Y-m-d', $varOne);
What i want is to know the date for next Mon, Tue, Wed, etc.
So if its Monday today i need to know the current date + 7, if its Tuesday i need to know current date +8, etc.
is there an easy way without a load of IF statments?
Thanks,
Mike
|
|
|
04-23-2008, 01:59 PM
|
#2 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,216
Thanks: 17
|
Quote:
Originally Posted by oMIKEo
Hi,
I'm trying to work out an easy way to calculate the date for next week based on the current date.
im using
PHP Code:
$varOne= mktime(0, 0, 0, date("m") , date("d"), date("Y")); $varTwo= date('Y-m-d', $varOne);
What i want is to know the date for next Mon, Tue, Wed, etc.
So if its Monday today i need to know the current date + 7, if its Tuesday i need to know current date +8, etc.
is there an easy way without a load of IF statments?
Thanks,
Mike
|
Using date()
PHP Code:
//I dont remember the date output codes, you will have to look them up. $next_week = date("Y-m-d",time()+(60*60*24*7));
Now that you have the date of next week from now, you can calculate the rest yourself
|
|
|
|
04-23-2008, 02:11 PM
|
#3 (permalink)
|
|
The Contributor
Join Date: Jan 2008
Location: Leeds
Posts: 52
Thanks: 7
|
Ive got it working but its a bit long winded:
PHP Code:
<?php
$myCurrentDayIs = date("D");
if($myCurrentDayIs == "Mon")
{
$day_for_mon = date("Y-m-d",time()+(60*60*24*7));
$day_for_tue = date("Y-m-d",time()+(60*60*24*8));
$day_for_wed = date("Y-m-d",time()+(60*60*24*9));
$day_for_thu = date("Y-m-d",time()+(60*60*24*10));
$day_for_fri = date("Y-m-d",time()+(60*60*24*11));
}
if($myCurrentDayIs == "Tue")
{
$day_for_mon = date("Y-m-d",time()+(60*60*24*6));
$day_for_tue = date("Y-m-d",time()+(60*60*24*7));
$day_for_wed = date("Y-m-d",time()+(60*60*24*8));
$day_for_thu = date("Y-m-d",time()+(60*60*24*9));
$day_for_fri = date("Y-m-d",time()+(60*60*24*10));
}
if($myCurrentDayIs == "Wed")
{
$day_for_mon = date("Y-m-d",time()+(60*60*24*5));
$day_for_tue = date("Y-m-d",time()+(60*60*24*6));
$day_for_wed = date("Y-m-d",time()+(60*60*24*7));
$day_for_thu = date("Y-m-d",time()+(60*60*24*8));
$day_for_fri = date("Y-m-d",time()+(60*60*24*9));
}
if($myCurrentDayIs == "Thu")
{
$day_for_mon = date("Y-m-d",time()+(60*60*24*4));
$day_for_tue = date("Y-m-d",time()+(60*60*24*5));
$day_for_wed = date("Y-m-d",time()+(60*60*24*6));
$day_for_thu = date("Y-m-d",time()+(60*60*24*7));
$day_for_fri = date("Y-m-d",time()+(60*60*24*8));
}
if($myCurrentDayIs == "Fri")
{
$day_for_mon = date("Y-m-d",time()+(60*60*24*3));
$day_for_tue = date("Y-m-d",time()+(60*60*24*4));
$day_for_wed = date("Y-m-d",time()+(60*60*24*5));
$day_for_thu = date("Y-m-d",time()+(60*60*24*6));
$day_for_fri = date("Y-m-d",time()+(60*60*24*7));
}
echo "$day_for_mon<br />$day_for_tue<br />$day_for_wed<br />$day_for_thu<br />$day_for_fri";
?>
|
|
|
04-23-2008, 02:53 PM
|
#4 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,216
Thanks: 17
|
I have a better way, I'll post it when I'm home and can debug it.
|
|
|
|
04-23-2008, 03:36 PM
|
#5 (permalink)
|
|
The Acquainted
Join Date: Nov 2007
Location: Netherlands
Posts: 112
Thanks: 11
|
why not just use strtotime?
PHP Code:
$date = strtotime('+1 week');
|
|
|
|
04-23-2008, 06:34 PM
|
#6 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,042
Thanks: 193
|
Quote:
Originally Posted by sjaq
why not just use strtotime?
PHP Code:
$date = strtotime('+1 week');
|
That's pretty much what I was going to be put.
__________________
VillageIdiot can have my babbies ;d
|
|
|
|
04-23-2008, 08:31 PM
|
#7 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,216
Thanks: 17
|
Thing is, he needs to know the dates of every day next week and has to be able to access them by day (monday, tuesday, ect). Those functions only say what next weeks today's date will be.
This will give you the dates of all the days next week, for a week.
PHP Code:
<?php //get todays ISO-8601 numeric represenation of the date $Today = date("N"); $DayNames = array(1=>"Monday",2=>"Tuesday",3=>"Wednesday",4=>"Thursday",5=>"Friday",6=>"Saturday",7=>"Sunday"); $Days = array();
$DayOffset = 0; //generate all the days for($i=0;$i<7;$i++) { if($DayOffset > 7-$Today) { $DayOffset = -($Today-1); } $Days[$DayNames[$Today+$DayOffset]] = date("Y-m-d",time()+(60*60*24*(7+$DayOffset))); $DayOffset++; }
echo $Days["Monday"] . "<br />"; echo $Days["Tuesday"] . "<br />"; echo $Days["Wednesday"] . "<br />"; echo $Days["Thursday"] . "<br />"; echo $Days["Friday"] . "<br />"; echo $Days["Saturday"] . "<br />"; echo $Days["Sunday"] . "<br />"; ?>
As of 4/23/08, it outputs
2008-04-28
2008-04-29
2008-04-30
2008-05-01
2008-05-02
2008-05-03
2008-05-04
|
|
|
|
04-23-2008, 11:30 PM
|
#8 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,239
Thanks: 3
|
How about an alternative approach: - Provide start date
- Find the following Monday
- Loop through next n days for display
In PHP that might look something like:
PHP Code:
<?php header('Content-Type: text/plain');
// Create timestamp from start date (use: "now" [or time() function] for current date/time )
$date = strtotime('23 April 2008');
// Move date to coming monday
$date = strtotime(sprintf('+%d day', 8 - date('N', $date)), $date);
// Write out the week's worth of dates
for ($days = 7; $days > 0; $date = strtotime('+1 day', $date), $days--)
{
echo date('D jS M Y', $date), "\n";
}
However, that global $date variable floating around doesn't help us to see what's going on. If only there was some OO way to do the same thing which encapsulated the date.
PHP Code:
<?php header('Content-Type: text/plain');
// Create DateTime object from start date (use: "now" [or no arguments] for current date/time)
$date = new DateTime('23 April 2008');
// Move date to coming monday
$date->modify(sprintf('+%d day', 8 - $date->format('N')));
// Write out the week's worth of dates
for ($days = 7; $days > 0; $date->modify('+1 day'), $days--)
{
echo $date->format('D jS M Y'), "\n";
}
Both strtotime and DateTime snippets output the same result:
Mon 28th Apr 2008
Tue 29th Apr 2008
Wed 30th Apr 2008
Thu 1st May 2008
Fri 2nd May 2008
Sat 3rd May 2008
Sun 4th May 2008
If it's not clear how/why I've done things the way that I have, please do ask! Either I, or someone quicker than me, can clear up any confusion.
P.S. The way I've written the for loops might seem a bit weird. Feel free to write them in a more 'normal' manner yourself.
__________________
salathe@php.net
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|