PDA

View Full Version : PHP Dates



SlowRoasted
September 7th, 2005, 02:37 PM
I need a php script that will get me the starting and ending date for the upcoming week, not the current week. Anyone have one? :beam:

Ankou
September 7th, 2005, 03:06 PM
<?php
$week_day = date('w');
$next_sunday = (7 - $week_day);
$next_saturday = $next_sunday + 6;
echo "Next week starts on: ";
echo date("l - F j, Y", strtotime("+$next_sunday day"));
echo "<br /><br />Next week ends on: ";
echo date("l - F j, Y", strtotime("+$next_saturday day"));
?>


$week_day - gives you the numerical day of the week (0 for Sunday, 1 for Monday... )

$next_sunday - if it's sunday then the next week is 7 days away. If it's Monday it's 6 days away. So it's really 7 - $the week_day

$next_saturday - is always 6 days away from the current sunday

Then we just use date() to format the time we want and use the time stamp that we create using PHP's strtotime().


I assumed you wanted to start the week on a Sunday and end it on a Saturday. If not, it's pretty easy to change.

Hope that's what you're looking for.

SlowRoasted
September 7th, 2005, 03:44 PM
Thanks Ankou, haven't tried it out yet but looks good:P

SlowRoasted
September 7th, 2005, 04:48 PM
how do i get the same info for the current week?

Ankou
September 7th, 2005, 05:08 PM
how do i get the same info for the current week?



<?php
$today = date('w');
$sunday = (7 - $today);
$saturday = $sunday + 6;

if($today == 0){ $get_sunday = "Sunday"; }else{ $get_sunday = "last Sunday"; }

echo "This week starts on: ";
echo date("l - F j, Y", strtotime($get_sunday));
echo "<br /><br />This week ends on: ";
echo date("l - F j, Y", strtotime("Saturday"));
?>



It's pretty much the same but we need to check the day of the week. If it's past Sunday then we have to get "last Sunday" -- otherwise it will get the upcoming Sunday. If it IS Sunday then we want the date for Sunday.

BTW - I didn't check this so you may want to run the script and test it out.

SlowRoasted
September 7th, 2005, 07:32 PM
Thanks man, your a life saver:P I'll check it when i get back to work.

Ankou
September 7th, 2005, 08:03 PM
No problem. When I first read your question I was thinking about all these different wats to do it. Then I remembered the power of strtotime() and it was a snap.

BTW - I checked and tested that second bit of code and it seems to work okay. If you encounter and problems and can't get it to work right just let me know.