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,299
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,299
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: 113
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,044
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,299
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,393
Thanks: 5
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 is offline  
Reply With Quote
Old 10-18-2012, 12:47 PM   #9 (permalink)
The Addict
 
Join Date: Oct 2012
Posts: 244
Thanks: 0
dashixiong is on a distinguished road
Default

Some conservatives have Coach Factory Outlet pushed that critique further, saying that Mr. Obama’s policies are too costly, often assist the wrong people Louis Vuitton Belts and could have the paradoxical effect of driving up college costs. The dispute turns not just on different Coach Factory Outlet assessments of how policies play out, but on differing philosophical views about the role of government. During Gucci Belts his time in office, Mr. Obama has sharply increased aid to low- and middle-income students, notably through the Pell Grant Coach Factory Outlet program, which grew from $14.6 billion given to 6 million students in 2008, to nearly $40 billion for Coach Factory Outlet almost 10 million students this year. His administration also made it easier to request aid, shortening the Coach Factory Online complex federal application and allowing people to transfer their financial information electronically from the Internal Coach Outlet Online Revenue Service database. But while many education experts laud his efforts, analysts of varying political Coach Outlet Online stripes have also questioned how much impact some of the president’s policies will have, noting that the prices Coach Online Outlet charged by colleges, and student borrowing, continue to climb.But behind the headlines about soaring costs, the Coach Factory Outlet Online reality is more complex and wildly uneven, because a growing number of students receive Coach Outlet Online financial aid, and only relatively high-income families pay those fast-rising sticker prices. Adjusted for Coach Factory Online inflation, the College Board calculates, the average net price changed little over the last decade at private Coach Factory Outlet schools, and rose only modestly at public ones.Defending federal spending, Arne Duncan, the secretary of Hermes Belts education, said that for more than 30 years, college prices had risen even when federal aid had not, leading him to believe Coach Factory Online there was zero correlation.
dashixiong is offline  
Reply With Quote
Old 01-29-2013, 09:35 AM   #10 (permalink)
The Addict
 
Join Date: Oct 2012
Posts: 244
Thanks: 0
dashixiong is on a distinguished road
Default

Organizers said Coach Outlet Online was opportune because the battle’s 150-year anniversary is in December, and Fredericksburg Coach Factory Outlet has been preparing to mark the sesquicentennial. in the new agreement is that Coach Outlet Online revolutionary councils from 14 Syrian provinces now each have a representative, though not all live Coach Online Outlet in Syria. The hope is that will bind the coalition to those inside the country. Perhaps Coach Bags Outlet the most important body the new group is expected to form is a Revolutionary Military Council Coach Factory Online to oversee the splintered fighting organizations and to funnel both lethal and nonlethal Coach Factory Outlet military aid to the rebels. It should unite units of the Free Syrian Army, various militias Coach Outlet Store Online and brigades in each city and large groups of defectors. Before the ink was even dry on the Coach Outlet Store final draft, negotiators hoped that it would bring them the antiaircraft missiles they crave to Coach Factory Stores take on the Syrian Air Force. The United States and Britain have offered only Coach Handbags Outlet nonmilitary aid to the uprising. A similar attempt by the Syrian National Council to Coach Factory Store supervise the military never jelled. Organizers said funding was too haphazard. Eventually foreign Coach Factory Online governments like Qatar and Saudi Arabia, which are financing and arming the rebels, found Coach Factory Online their own favorite factions to deal with. Foreign leaders notably including Secretary of State Coach Outlet Hillary Rodham Clinton urged this unification largely so they could coordinate their Coach Factory Outlet efforts and aid through a group of technocrats. Once it receives international recognition, the Coach Outlet Store Online coalition is supposed to establish a temporary Coach Outlet Online military never jelled.
dashixiong 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 09:17 PM.

 
     

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