0

Ok, I'm not exactly great at PHP, but I'm learning...

Here's what I would like to do:

An event occurs every Tuesday and Sunday. I would like to echo out the "next" date for the event.

For example-

"Yardsale every Tuesday and Saturday
Next Yardsale: " [echo the next event day, say 'May 29']

I have a good idea of what I want to do, but the actual code eludes me.

- check today
- is today a tues or sunday?
- if yes, echo date
- if no, is today+1 a tuesday or sunday?
- etc...

Any help would be greatly appreciated.

thanks!

5
Contributors
12
Replies
16
Views
6 Years
Discussion Span
Last Post by verbob
0

This code does what you've asked, and starts looking from "tomorrow".

<?php
$dt = new DateTime("tomorrow",new DateTimeZone("Pacific/Auckland"));
$di = new DateInterval("P1D");
while ( ($dt -> format("l") != 'Tuesday') && ($dt -> format("l") != 'Saturday') ) {
  $dt -> add($di);
  }
echo 'Next sale: '.$dt->format("l, j F Y");
?>

Just replace the timezone with that of your locale, and of course you can change the format of the output as well.

Edited by edwinhermann: n/a

0

<?php
$date = strtotime("now");
if (date('w', strtotime($date)) == 2 || date('w', strtotime($date)) == 6 )
{
echo date_format($date, "Y-m-d");
}
else if(date('w', strtotime($date)) < 2)
{
$nextTuesday = strtotime("this Tuesday");
echo date_format($nextTuesday, "Y-m-d");
}
else
{
$nextSaturday = strtotime("this Saturday");
echo date_format($nextSaturday, "Y-m-d");
}
?>

0

Thanks!

edwinhermann, I get this error with your code:
"Fatal error: Class 'DateInterval' not found "

Tessy, I get this error with your code:
"Warning: date_format() expects parameter 1 to be DateTime, integer given "
[ referring to this line: echo date_format($nextTuesday, "Y-m-d"); ]

Edited by verbob: n/a

0

I think you are looking for this.

<?php
$firstday="Tuesday";
$secondday="Saturday";

$today=date("l");

$nextdate1=strtotime("next $firstday");
$nextdate2=strtotime("next $secondday");


if ($today==$firstday || $today==$secondday)
{
	echo "Today: ".date("m-d-Y")."<br>";
}
if ($nextdate1<$nextdate2)
{
	echo "Next: ".date("m-d-Y l",$nextdate1)."<br>";
	echo "Next: ".date("m-d-Y l",$nextdate2)."<br>";
}
else
{
 	echo "Next: ".date("m-d-Y l",$nextdate2)."<br>";
	echo "Next: ".date("m-d-Y l",$nextdate1)."<br>";
}


?>
0

Thanks!

edwinhermann, I get this error with your code:
"Fatal error: Class 'DateInterval' not found "

You're probably on an older version of PHP. You'll need 5.3+ for my code.

You may want to try other code offered by other people, though they use "date()" which is best to avoid, so when you upgrade to PHP5.3 you may want to transition your code back to something like what I posted, using the DateTime andDateInterval classes.

0

I did this, looks less elegant than the others though:

$chosen_days = array(4,2); //populate this from a form or a DB (4 = Thu, 2 = Tue)
$today = date("w");
for($x=0;$x<7;$x++){
	$to_next = $x - $today;
	if($to_next < 0)$to_next +=	7;
	$diff[$x] = $to_next;
}
asort($diff);
foreach($diff as $key => $val){
	if(in_array($key,$chosen_days)){
		echo "Next occurrence is " . date("l jS \of F Y",mktime(0,0,0,date('n'),date('j') + $val,date('Y')));
		break;
	}
}

The only advantage of this is that you don't have to hard-code any names into an OR statement. If your days come from a form or DB:

$chosen_days = (array)$_POST['days']; //will accept as an array of values e.g. checkboxes

Edited by diafol: n/a

0

Thanks all!

Tessy - looks like you have got it :)

Just a matter of adjusting the output, but works for what I need. Thanks!!!

0

JUST TO NOTE:

tessy, the code "half" worked (always seemed to show sunday)

I made a few adjustments once I figured out how it worked, and it seems to be ok now (Guess I will find out for sure on Wednesday ...and Sunday is 0 not 6 by the way, according to php.net).

$date = strtotime("now");
switch ($date) {
	case (date('w', strtotime($date)) == 2 || date('w', strtotime($date)) == 0):
		echo date("D M j",$date);
		break;
	case (date('w', strtotime($date)) > 0):
		echo date("D M j",strtotime("this Tuesday"));
		break;
	case (date('w', strtotime($date)) > 2):
		echo date("D M j", strtotime("this Sunday"));
		break;
	}

it looks illogical - the 3rd case should be second ... logically anyway... but it only echo's sunday if I do it in that order... strange indeed.

Edited by verbob: n/a

0

Have you tried my code posted above, I am again posting it.

<?php
$firstday="Tuesday";
$secondday="Saturday";

$today=date("l");

$nextdate1=strtotime("next $firstday");
$nextdate2=strtotime("next $secondday");


if ($today==$firstday || $today==$secondday)
{
	echo "Today: ".date("m-d-Y")."<br>";
}
if ($nextdate1<$nextdate2)
{
	echo "Next: ".date("m-d-Y l",$nextdate1)."<br>";
	echo "Next: ".date("m-d-Y l",$nextdate2)."<br>";
}
else
{
 	echo "Next: ".date("m-d-Y l",$nextdate2)."<br>";
	echo "Next: ".date("m-d-Y l",$nextdate1)."<br>";
}


?>
0

JUST TO NOTE:

tessy, the code "half" worked (always seemed to show sunday)

I made a few adjustments once I figured out how it worked, and it seems to be ok now (Guess I will find out for sure on Wednesday ...and Sunday is 0 not 6 by the way, according to php.net).

Somewhere in your first post in this thread, you had mentioned you want next Tuesday or Saturday. That's why I mentioned 2 and 6. Missed out the Sunday part. Sorry :)

0

No worries. And thanks. Had to change the code substantially to get it to work properly on my server for some strange reason, but thanks to your code, I managed to figure out how to do it :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.