Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dates & Weekday Names 1

Status
Not open for further replies.

smashing

Programmer
Joined
Oct 15, 2002
Messages
170
Location
US
What I'm trying to do is allow people to choose dates of travel in the next 30 days only, and only Thursdays & Sundays.
I'm thinking:

$today = date("Y-m-d");
$today = strtotime($today);

then spliting it up & using gregoriantojd to get day of week.

Before I really delve into this, do any of you have an easier way?
 
yes, much easier... one moment and I should have something for you...

 
Ok, well in the spirit of tek-tips, here's a start :) This will get you the next 5 thursdays (in this case including today)... it won't check that that's within 30 days, but that's a pretty easy check to add in... all I'm doing is leveraging strototime to get the timestamp of the next thursday, then adding 7 days at a time.

Then, I loop through those timestamps and format them.

<?php
$day[0]=strtotime(&quot;thursday&quot;); //seed the time
$day[1]=mktime(0,0,0,date(&quot;m&quot;, $day[0]), date(&quot;d&quot;, $day[0])+7, date(&quot;Y&quot;, $day[0]));
$day[2]=mktime(0,0,0,date(&quot;m&quot;, $day[0]), date(&quot;d&quot;, $day[0])+14, date(&quot;Y&quot;, $day[0]));
$day[3]=mktime(0,0,0,date(&quot;m&quot;, $day[0]), date(&quot;d&quot;, $day[0])+21, date(&quot;Y&quot;, $day[0]));
$day[4]=mktime(0,0,0,date(&quot;m&quot;, $day[0]), date(&quot;d&quot;, $day[0])+28, date(&quot;Y&quot;, $day[0]));

foreach ($day as $index=>$time) {
$formatted_days[$index]=date(&quot;Y-m-d&quot;, $time);
}
echo '<pre>';
print_r($formatted_days);
?>
 
In the spirit of tek-tips:
Thanks Man!!
 
Sorry to keep lengthening the thread... I might point out that you accomplish this with some neater looking code by adding the right number of seconds to the timestamp... however, I avoided this method for the &quot;spring ahead&quot; time, when your timestamp would fall back and start giving you wednesdays.

This could be easily circumvented by using noon on thursday instead of midnight (the default when using strtotime), but in case you ever wanted to have more granualarity than Y-m-d I figured this was the better way to go.

-Rob
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top