TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 04-23-2008, 01:55 PM   #1 (permalink)
The Contributor
 
oMIKEo's Avatar
 
Join Date: Jan 2008
Location: Leeds
Posts: 52
Thanks: 7
oMIKEo is on a distinguished road
Default 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:
$varOnemktime(000date("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
Send a message via MSN to oMIKEo
oMIKEo is offline  
Reply With Quote
Old 04-23-2008, 01:59 PM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,216
Thanks: 17
Village Idiot is on a distinguished road
Default

Quote:
Originally Posted by oMIKEo View Post
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:
$varOnemktime(000date("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
Village Idiot is offline  
Reply With Quote
Old 04-23-2008, 02:11 PM   #3 (permalink)
The Contributor
 
oMIKEo's Avatar
 
Join Date: Jan 2008
Location: Leeds
Posts: 52
Thanks: 7
oMIKEo is on a distinguished road
Default

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";

?>
Send a message via MSN to oMIKEo
oMIKEo is offline  
Reply With Quote
Old 04-23-2008, 02:53 PM   #4 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,216
Thanks: 17
Village Idiot is on a distinguished road
Default

I have a better way, I'll post it when I'm home and can debug it.
Village Idiot is offline  
Reply With Quote
Old 04-23-2008, 03:36 PM   #5 (permalink)
The Acquainted
 
sjaq's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 112
Thanks: 11
sjaq is on a distinguished road
Default

why not just use strtotime?

PHP Code:
$date strtotime('+1 week'); 
sjaq is offline  
Reply With Quote
Old 04-23-2008, 06:34 PM   #6 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,042
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by sjaq View Post
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
Orc is offline  
Reply With Quote
Old 04-23-2008, 08:31 PM   #7 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,216
Thanks: 17
Village Idiot is on a distinguished road
Default

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
Village Idiot is offline  
Reply With Quote
Old 04-23-2008, 11:30 PM   #8 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,239
Thanks: 3
Salathe is on a distinguished road
Default

How about an alternative approach:
  1. Provide start date
  2. Find the following Monday
  3. 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'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'$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
Salathe is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 01:15 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design