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 06-28-2009, 01:21 PM   #1 (permalink)
The Wanderer
 
Join Date: Dec 2007
Posts: 8
Thanks: 2
Jonnee is on a distinguished road
Default Days of running week

Anyone have tips for following function:

I need to display running week days in numbers to seperate fields. I.E.

Week 23 ->
Table 1 -> 21.6.
Table 2 -> 22.6.
Table 3 -> 23.6.
Table 4 -> 24.6.
Table 5 -> 25.6.

And they shouldn't change beaforee week changes.

Another problem which may occur, when month changes during the week.. So got any solutions for this, dear talkphp members?


Regards, Jonne
Jonnee is offline  
Reply With Quote
Old 06-28-2009, 05:30 PM   #2 (permalink)
The Contributor
 
Runar's Avatar
 
Join Date: Nov 2008
Location: Norway
Posts: 58
Thanks: 20
Runar is on a distinguished road
Default

You could use the date() function to either display the current week number, or get the week number from a string:

PHP Code:
echo date'W'$datestring );
// Prints something like: 42

echo date'W' );
// Prints current week number: 26 
I hope this helps!

Edit: I misunderstood your question. I will try to think out a new solution.


Yours, Runar
Send a message via MSN to Runar
Runar is offline  
Reply With Quote
Old 06-28-2009, 09:02 PM   #3 (permalink)
The Wanderer
 
Join Date: Dec 2007
Posts: 8
Thanks: 2
Jonnee is on a distinguished road
Default

Yes getting the week number isn't the problem as you noticed little later.

The problem is to get numbers of the week.

Lets say I have string $week and it contains current week number and days in array i.e.
$week [
week -> 23
sun -> 21
mon -> 22
tue -> 23
wed -> 24
thu -> 25
fri -> 26
sat -> 27
]

that would be the goal of the function!
Jonnee is offline  
Reply With Quote
Old 06-28-2009, 11:02 PM   #4 (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

There are a number of ways to gather a series of dates. Here are three different code snippets which all produce the same array (see below the code) but are written using different tools.

Note on "sunday last week"
All of the snippets use a string to determine the Sunday at the beginning of this week which may be confusing! If today is Sunday, the string will mean today and not "last Sunday" as you might first assume. This has to do with how strtotime (and the underlying C function get_date) parses such strings. Using Sunday June 28th as an example, "Sunday" means the next Sunday from now (ie, July 5th) and "last week" means take away a week from that Sunday (ie, June 28th).

Onto the snippets. Note that the first two have been formatted to behave more like the third and may not be how one would normally approach this with basic loops and date functions.
  1. PHP Code:
    // Plain old functions
    $begin    strtotime('sunday last week');
    $interval '+1 day';
    $period   6;

    $array = array('week' => date('W'$begin));
    for (
    $day = clone $begin$i 0$i <= $period$i++)
    {
        
    $array[strtolower(date('D'$day))] = date('j M'$day);
        
    $day strtotime($interval$day);
    }
    print_r($array); 
  2. PHP Code:
    // PHP 5.2 DateTime
    $begin    = new DateTime('sunday last week');
    $interval '+1 day';
    $period   6;

    $array = array('week' => $begin->format('W'));
    for (
    $day $begin$i 0$i <= $period$i++)
    {
        
    $array[strtolower($day->format('D'))] = $day->format('j M');
        
    $day->modify($interval);
    }
    print_r($array); 
  3. PHP Code:
    // PHP 5.3 DateTime DateInterval DatePeriod
    $begin    = new DateTime('sunday last week');
    $interval DateInterval::createFromDateString('+1 day');
    $period   = new DatePeriod($begin$interval6);

    $array = array('week' => $begin->format('W'));
    foreach (
    $period as $day)
    {
        
    $array[strtolower($day->format('D'))] = $day->format('j M');
    }
    print_r($array); 

They all (should) print the same array:
Code:
Array
(
    [week] => 26
    [sun] => 28 Jun
    [mon] => 29 Jun
    [tue] => 30 Jun
    [wed] => 1 Jul
    [thu] => 2 Jul
    [fri] => 3 Jul
    [sat] => 4 Jul
)
Personally, I can't wait to adopt those PHP 5.3 classes as they really make dealing with dates much more simple and easy to understand what is going on.

Last edited by Salathe : 06-29-2009 at 08:22 AM. Reason: Added note on code formatting
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
Jonnee (06-29-2009)
Old 06-29-2009, 10:03 AM   #5 (permalink)
The Wanderer
 
Join Date: Dec 2007
Posts: 8
Thanks: 2
Jonnee is on a distinguished road
Default

Works flawless! Thank you Salathe for your help!

I hope this topic helped someone other also!

I will add also the day higlighter but that is too simple to ask :)
Jonnee 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

Similar Threads
Thread Thread Starter Forum Replies Last Post
date/mktime for next week? oMIKEo Absolute Beginners 9 01-29-2013 09:35 AM
Calendar Script - Week View oMIKEo Absolute Beginners 2 09-05-2008 09:11 PM
Away for a week Alan @ CIT The Lounge 9 02-08-2008 10:42 PM
Running a Server CMellor The Lounge 9 12-31-2007 10:15 PM
PHP Tutorial Section Up and Running CreativeLogic News and Announcements 4 06-01-2005 06:32 PM


All times are GMT. The time now is 08:52 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