SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Jan 2001
    Posts
    244
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Get day from timestamp & add days if weekend

    Hi Everyone,

    I need to compare if a date from a timestamp is on a weekend (Saturday or Sunday) and add necessary days to the variable to make it a weekday.

    This is the code I have now:

    Code PHP:
    $month = "6";
    $day = "12";
    $incrementBy = "1";
    $year = "2007";
     
    $current_stamp = mktime(0,0,0,$month,$day+$incrementBy,$year);
    $exact_current_stamp = mktime($hour,$minute,0,$month,$day+=$incrementBy,$year);

    With that code, the variables $current_stamp and $exact_current_stamp get an value of 1181703600 (as of today). I need to check when those values get a weekend value and add the appropriate days to become a weekday.

    For example, lets say that the value of the timestamp gives me a Saturday, there I know I need to add 2 days to that timestamp. And if it gives me a Sunday, it needs to add 1 day to the timestamp.

    What else do I need to add to accomplish this?

    Thanks!
    Guillermo

  2. #2
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Location
    San Antonio, TX
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This isn't totally correct, but it should get you started:

    PHP Code:
    if date("D"mktime(000$month$day$year)); = 'Sat' {
        
    mktime(000date("m"), date("d")+2date("Y"));
    }elseif 
    date("D"mktime(000$month$day$year)); = 'Sun' {
        
    mktime(000date("m"), date("d")+1date("Y"));

    The 'then' parts of those if then statements will probably need some work, but the 'if' parts should do what you're needing.
    Last edited by webgambit; Jun 13, 2007 at 09:50. Reason: forgot the code tags. :(

  3. #3
    SitePoint Evangelist Andrewaclt's Avatar
    Join Date
    Dec 2003
    Location
    Raleigh, NC
    Posts
    535
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use [php] blocks for crying out loud.

  4. #4
    SitePoint Addict Trent Reimer's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    webgambit has the right idea. You can also get rid of most of the function calls to boost performance:

    PHP Code:
    $exact_current_stamp mktime($hour,$minute,0,$month,$day+=$incrementBy,$year);
    // Subtract the total seconds of the $hour and $minute variables
    $current_stamp $exact_current_stamp - (($hour 60 $minute) * 60);

    if ((
    $weekday date('D'$current_stamp)) == 'Sat') {
            
    // Add two day's worth of seconds to the timestamp
            
    $current_stamp += 172800// 48 hours * 60 minutes * 60 seconds = 172800 seconds
    } elseif ($weekday == 'Sun') {
            
    // Add one day's worth of seconds
            
    $current_stamp += 86400;



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •