Page 1 of 1

Find date help?

Posted: Mon Sep 08, 2008 5:02 pm
by color0572
will anyone please help?

Here is i would like to do.

If I know the starting date.
How can I find Mon, Wed & Fri in two weeks from my starting date eg startingdate is (2008-09-15)
and find Tuesday, Thurday & Sathurday from my starting date eg startingdate is (2008-09-16)
and Mon - Fri from my starting date eg startingdate is (2008-09-15)

Thank you very much for your help

color,

Re: Find date help?

Posted: Mon Sep 08, 2008 5:09 pm
by marcth
http://ca3.php.net/manual/en/function.date.php

Code: Select all

 
<?php
/*
@params $firstdate, $lastdate
@return array() of array(monday,sunday)
@description returns all the mondays and sundays of the given date range
*/
function get_week_intervals($fdate,$ldate)
{
    list($year,$month,$day) = explode('-',$fdate);
    $daynum = date('w',
                   mktime(date('H'),
                          date('i'),
                          date('s'),
                          $month,
                          $day,
                          $year)
                  );
    $daynum = $daynum==0? 7 : $daynum;
    $week=array();
    //get the dayname of the first day
    //if month = current month get the current date as the last day
    if($month==date('m'))
    {
        $lastday = date('d');
    }
    else
    {
        $lastday = date('t', strtotime($fdate));
    }
    if((date('l',strtotime($fdate))) == 'Sunday')
    {
        $monday = $fdate;
        $sunday = $fdate;
    }
    else
    {
        $monday = $fdate;
        $sunday = date('Y-m-d',(mktime(date('H'),
                       date('i'),date('s'),$month,
                       $day,$year))-($daynum-7)*86400);
 
    }
    $week[] = array('monday'=>$monday,'sunday'=>$sunday);
 
    $day = date('d',strtotime($sunday." +1 day"));
 
    while($sunday < $ldate)
    {
        $monday = date('Y-m-d',strtotime($sunday." +1 day"));
 
        list($year,$month,$day) = explode('-',$monday);
        $daynum = date('w',
                      mktime(date('H'),
                             date('i'),
                             date('s'),
                             $month,
                             $day,
                             $year)
                       );
        $daynum = $daynum==0? 7 : $daynum;
 
        $sunday = date('Y-m-d',(mktime(date('H'),date('i'),
                       date('s'),$month,$day,$year))-($daynum-7)*86400);
        if($sunday > $ldate)
        {
            $sunday = $ldate;
        }
 
        $week[] = array('monday'=>$monday,'sunday'=>$sunday);
    }
 
    return $week;
}
?>
 

Re: Find date help?

Posted: Tue Sep 09, 2008 9:25 am
by color0572
Thank you so much for your quick respond, I will give it a try and let you know
Thanks again Marc!

Re: Find date help?

Posted: Tue Sep 09, 2008 11:25 am
by color0572
Hi Mac - I did try this function like below and the result show = Array
Can you give some more adviser here,
Thanks alot


function get_week_intervals($fdate,$ldate)
{
list($year,$month,$day) = explode('-',$fdate);
$daynum = date('w',
mktime(date('H'),
date('i'),
date('s'),
$month,
$day,
$year)
);
$daynum = $daynum==0? 7 : $daynum;
$week=array();
//get the dayname of the first day
//if month = current month get the current date as the last day
if($month==date('m'))
{
$lastday = date('d');
}
else
{
$lastday = date('t', strtotime($fdate));
}
if((date('l',strtotime($fdate))) == 'Sunday')
{
$monday = $fdate;
$sunday = $fdate;
}
else
{
$monday = $fdate;
$sunday = date('Y-m-d',(mktime(date('H'),
date('i'),date('s'),$month,
$day,$year))-($daynum-7)*86400);

}
$week[] = array('monday'=>$monday,'sunday'=>$sunday);

$day = date('d',strtotime($sunday." +1 day"));

while($sunday < $ldate)
{
$monday = date('Y-m-d',strtotime($sunday." +1 day"));

list($year,$month,$day) = explode('-',$monday);
$daynum = date('w',
mktime(date('H'),
date('i'),
date('s'),
$month,
$day,
$year)
);
$daynum = $daynum==0? 7 : $daynum;

$sunday = date('Y-m-d',(mktime(date('H'),date('i'),
date('s'),$month,$day,$year))-($daynum-7)*86400);
if($sunday > $ldate)
{
$sunday = $ldate;
}

$week[] = array('monday'=>$monday,'sunday'=>$sunday);
}

return $week;

}
echo get_week_intervals("2007-08-08","2008-08-08");

Re: Find date help?

Posted: Tue Sep 09, 2008 11:39 am
by marcth
I didn't really look at the code I posted--I just copied it from the PHP.net site.

It appears like the returned value is an array. try outputting it like:

Code: Select all

 
echo '<pre>';
print_r(get_week_intervals("2007-08-08","2008-08-08"));
echo '</pre>';
 

Re: Find date help?

Posted: Tue Sep 09, 2008 11:50 am
by color0572
it's working,
Thank you so much!!!!! Marc,

Re: Find date help?

Posted: Thu Sep 11, 2008 12:39 pm
by color0572
Hi Marc, I really need your help again. Base on this example I try to find Monday, Wednesday, and Friday, But I could not have it to work, Will you help me on this,
Thanks alot,
color

Re: Find date help?

Posted: Thu Sep 11, 2008 8:58 pm
by marcth
Take a look at the php.net website; and you will understand everything!